Fork me on GitHub

winform——windows 服务的安装 卸载 启动 停止

安装服务和卸载服务采用process启动命令行的方式。启动服务和停止服务用到了 System.ServiceProcess.ServiceController这个类。
 
程序是这样运行的,首先启动服务和停止服务会去判断服务是否存在,根据的是服务名称,而这个名称我卸载App.config文件下,通过appsettings加key和value,程序在load事件里读取这个名称,如果不存在,就不执行相应功能。
 
当这个服务存在了之后,在去判断服务所处的状态,
if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
满足这个条件,才能启动服务。
 
停止服务满足这样的条件:
if (star_service.Status == ServiceControllerStatus.Running),就会执行停止服务的代码。
 
安装与卸载,是通过这个工具:InstallUtil.exe,这个工具需要根据服务所对应的.net framework版本来确定 。
 
如果是3.5及以下的版本要使用这个文件夹下的:C:\Windows\Microsoft.NET\Framework\v2.0.50727
 
如果是4.0及以上的版本写的服务要用这个文件夹下的:C:\Windows\Microsoft.NET\Framework\v4.0.30319
 
下面给出封装的命令行,只需传个服务所在路径就可以了。
 private string excuteCmd(string strCMD)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            //这里根据需要安装服务的框架版本不同,路径也不同,我的是.net3.5的
            p.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727";

            //p.StartInfo.CreateNoWindow = true;
            //p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.Start();//启动程序
            
            p.StandardInput.WriteLine(strCMD);
            p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine(" exit");
            p.StandardInput.AutoFlush = true;


            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();

            return output;
        }
在给个启动服务的代码:
private string excuteCmd(string strCMD)
        {

            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            //这里根据需要安装服务的框架版本不同,路径也不同,我的是.net3.5的
            p.StartInfo.WorkingDirectory = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727";

            //p.StartInfo.CreateNoWindow = true;
            //p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            p.Start();//启动程序
            
            p.StandardInput.WriteLine(strCMD);
            p.StandardInput.AutoFlush = true;
            p.StandardInput.WriteLine(" exit");
            p.StandardInput.AutoFlush = true;


            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();

            return output;
        }

在给个启动服务的代码:
 
   private void btn_startService_Click(object sender, EventArgs e)
        {
            if (!IsServerExists(serviceName))
            {
                MessageBox.Show("未找到配置文件写的服务:" + serviceName);
                return;
            }
            bool isStarted = false;
            ServiceController star_service = new ServiceController(serviceName);
            if (star_service.Status != ServiceControllerStatus.Running &&
star_service.Status != ServiceControllerStatus.StartPending)
            {
                star_service.Start();
//这里是看80s内,服务是否启动完毕
                for (int i = 0; i < 80; i++)
                {
                    star_service.Refresh();
                    System.Threading.Thread.Sleep(1000);
                    if (star_service.Status == ServiceControllerStatus.Running)
                    {
                        isStarted = true;
                        break;
                    }
                    if (i == 79)
                    {
                        isStarted = false;
                    }
                }
            }
            else
            {
                MessageBox.Show("服务已经启动或被暂停");
                return;
            }
            if (isStarted)
            {
                MessageBox.Show("服务启动完成!");
            }
            else
            {
                MessageBox.Show("服务未在80秒内启动起来,请到服务里去手动启动");
            }

        }
View Code
卸载服务:
string strCMD = @"InstallUtil.exe /u " + txt_filePath.Text;
string output = excuteCmd(strCMD);
安装服务:

string strCMD = @"InstallUtil.exe " + txt_filePath.Text;

string output = excuteCmd(strCMD);

代码下载

InstallService.zip

posted @ 2020-11-06 15:10  HelloLLLLL  阅读(242)  评论(0编辑  收藏  举报