Windows服务安装、卸载、启动和关闭的管理器

      最近在重构公司的系统,把一些需要独立执行、并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了。但是每次都要建立Windows服务项目,编写服务代码,建立服务的安装程序,然后还要通过InstallUtil.exe这个命令来安装Windows服务,如果要是想卸载也要使用这个命令,只是在InstallUtil.exe这个命令后增加一个参数/u,表示卸载,我相信大家都对这个很熟悉了,我就不多说了。

      我为了不想使用这个命令来安装和卸载Windows服务,我就自己写了一个工具类,已经完全经过了单元测试,完全靠谱,大家可以放心使用。话不多说,直接上代码,代码有注释,其实也不是很难,相信大家都能看的懂。

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Configuration.Install;
  5 using System.IO;
  6 using System.Linq;
  7 using System.ServiceProcess;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 
 11 namespace Enterprise.Framework.Utils
 12 {
 13     /// <summary>
 14     /// Windows服务实例的管理器,可以安装、启动、关停和卸载Windows服务实例
 15     /// </summary>
 16     public static class WindowsServiceInstanceManager
 17     {
 18         /// <summary>
 19         /// 判断指定的名称的Windows服务是否存在
 20         /// </summary>
 21         /// <param name="serviceName">Windows服务的名称</param>
 22         /// <returns>返回布尔值,true表示指定名称的Windows服务存在,false表示指定名称的Windows服务不存在</returns>
 23         public static bool IsServiceExisted(string serviceName)
 24         {
 25             ServiceController[] services = ServiceController.GetServices();
 26             foreach (ServiceController controller in services)
 27             {
 28                 if (string.Compare(controller.ServiceName, serviceName, true) == 0)
 29                 {
 30                     return true;
 31                 }
 32             }
 33             return false;
 34         }
 35 
 36         /// <summary>
 37         /// 安装Windows服务,如果存在同名的服务,也认为安装时成功的
 38         /// </summary>
 39         /// <param name="serviceFilePath">要安装的Windows服务的文件的地址</param>
 40         /// <param name="serviceName">要安装的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务存在,就不需要安装</param>
 41         /// <returns>返回布尔值,true表示安装Windows服务成功,false表示安装Windows服务失败</returns>
 42         public static bool InstallService(string serviceFilePath, string serviceName)
 43         {
 44             if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath))
 45             {
 46                 return false;
 47             }
 48             if (!File.Exists(serviceFilePath))
 49             {
 50                 return false;
 51             }
 52             if (this.IsServiceExisted(serviceName))
 53             {
 54                 return true;
 55             }
 56             using (AssemblyInstaller installer = new AssemblyInstaller())
 57             {
 58                 try
 59                 {
 60                     installer.UseNewContext = true;
 61                     installer.Path = serviceFilePath;
 62                     IDictionary savedState = new Hashtable();
 63                     installer.Install(savedState);
 64                     installer.Commit(savedState);
 65                     return true;
 66                 }
 67                 catch (Exception)
 68                 {
 69                     throw;
 70                 }
 71             }
 72         }
 73 
 74         /// <summary>
 75         /// 卸载Windows服务,如果该名称的Windows服务不存在,也认识卸载失败
 76         /// </summary>
 77         /// <param name="serviceFilePath">要卸载的Windows服务文件的地址</param>
 78         /// <param name="serviceName">要卸载的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务不存在,就不需要卸载</param>
 79         /// <returns>返回布尔值,true表示卸载Windows服务成功,false表示卸载Windows服务失败</returns>
 80         public static bool UninstallService(string serviceFilePath, string serviceName)
 81         {
 82             if (string.IsNullOrEmpty(serviceFilePath) || string.IsNullOrWhiteSpace(serviceFilePath))
 83             {
 84                 return false;
 85             }
 86             if (!File.Exists(serviceFilePath))
 87             {
 88                 return false;
 89             }
 90             if (!IsServiceExisted(serviceName))
 91             {
 92                 return false;
 93             }
 94             using (AssemblyInstaller installer = new AssemblyInstaller())
 95             {
 96                 try
 97                 {
 98                     installer.UseNewContext = true;
 99                     installer.Path = serviceFilePath;
100                     installer.Uninstall(null);
101                     return true;
102                 }
103                 catch (Exception)
104                 {
105                     throw;
106                 }
107             }
108         }
109 
110         /// <summary>
111         /// 启动Windows服务
112         /// </summary>
113         /// <param name="serviceName">要启动的Windows服务的名称</param>
114         /// <returns>返回布尔值,true表示启动Windows服务成功,false表示启动Windows服务失败</returns>
115         public static bool StartService(string serviceName)
116         {
117             if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))
118             {
119                 return false;
120             }
121             using (ServiceController control = new ServiceController(serviceName))
122             {
123                 try
124                 {
125                     if (control.Status == ServiceControllerStatus.Stopped)
126                     {
127                         control.Start();
128                     }
129                     return true;
130                 }
131                 catch (Exception)
132                 {
133                     throw;
134                 }
135             }
136         }
137 
138         /// <summary>
139         /// 关停Windows服务
140         /// </summary>
141         /// <param name="serviceName">要关停Windows服务的名称</param>
142         /// <returns>返回布尔值,true表示关停Windows服务成功,false表示关停Windows服务失败</returns>
143         public static bool StopService(string serviceName)
144         {
145             if (string.IsNullOrEmpty(serviceName) || string.IsNullOrWhiteSpace(serviceName))
146             {
147                 return false;
148             }
149             using (ServiceController control = new ServiceController(serviceName))
150             {
151                 try
152                 {
153                     if (control.Status == ServiceControllerStatus.Running)
154                     {
155                         control.Stop();
156                     }
157                     return true;
158                 }
159                 catch (Exception)
160                 {
161                     throw;
162                 }
163             }
164         }
165     }
166 }


      好了,这就是今天自己的一点小作品,每天进步一点点,努力坚持。不忘初心,继续努力吧,欢迎大家前来讨论。

猜你喜欢

转载自www.cnblogs.com/PatrickLiu/p/9402532.html