最近在重构公司的系统,把一些需要独立执行、并不需要人为关注的组件转换为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 ///14 /// Windows服务实例的管理器,可以安装、启动、关停和卸载Windows服务实例 15 /// 16 public static class WindowsServiceInstanceManager 17 { 18 ///19 /// 判断指定的名称的Windows服务是否存在 20 /// 21 /// Windows服务的名称 22 ///返回布尔值,true表示指定名称的Windows服务存在,false表示指定名称的Windows服务不存在 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 ///37 /// 安装Windows服务,如果存在同名的服务,也认为安装时成功的 38 /// 39 /// 要安装的Windows服务的文件的地址 40 /// 要安装的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务存在,就不需要安装 41 ///返回布尔值,true表示安装Windows服务成功,false表示安装Windows服务失败 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 ///75 /// 卸载Windows服务,如果该名称的Windows服务不存在,也认识卸载失败 76 /// 77 /// 要卸载的Windows服务文件的地址 78 /// 要卸载的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务不存在,就不需要卸载 79 ///返回布尔值,true表示卸载Windows服务成功,false表示卸载Windows服务失败 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 ///111 /// 启动Windows服务112 /// 113 /// 要启动的Windows服务的名称114 ///返回布尔值,true表示启动Windows服务成功,false表示启动Windows服务失败 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 try124 {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 ///139 /// 关停Windows服务140 /// 141 /// 要关停Windows服务的名称142 ///返回布尔值,true表示关停Windows服务成功,false表示关停Windows服务失败 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 try152 {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 }