博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WindowService

Posted on 2012-06-12 11:07  生命如风  阅读(213)  评论(0编辑  收藏  举报

1、参考网上的代码,自己写了个检测进程的服务。

           启动的飞鸽只能看到进程,看不到图形见面。

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("d:\\log.txt", true))
            {
                Process[] processList;
                processList = System.Diagnostics.Process.GetProcesses();

                IList<string> processNames = new List<string>();
                //Process process;
                foreach (Process process in processList)
                {
                    processNames.Add(process.ProcessName);
                }

                if (processNames.Contains("IPMSG2007"))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "飞鸽——程序已经启动");
                }
                else
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "飞鸽——程序没有启动");

                    //没有启动,则启动飞鸽

                    System.Diagnostics.ProcessStartInfo info = new ProcessStartInfo();

                    info.FileName = "IPMSG2007.exe";
                    info.WindowStyle = ProcessWindowStyle.Maximized;
                    info.WorkingDirectory = @"E:\tool\";

                    Process proMsg;
                    try
                    {
                        proMsg = Process.Start(info);
                        System.Threading.Thread.Sleep(500);
                    }
                    catch (System.ComponentModel.Win32Exception ex)
                    {

                        sw.WriteLine(ex.Message);
                    }
                }
            }

2、使用批处理安装服务。(在网上找的)

     %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe WindowsServiceDemo.exe
     Net Start Service1
     sc config Service1 start=atuo 
     pause

3、使用安装项目安装服务,在安装完成后启动服务

   

        //安装结束
        protected override void OnAfterInstall(System.Collections.IDictionary savedState)
        {
            //安装结束后 启动服务
            ServiceController serviceController = new ServiceController("Service1");

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("d:\\log.txt", true))
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "服务状态:" + serviceController.Status.ToString());
            }
            serviceController.Start();
        }