最近在重构公司的系统,把一些需要独立执行、并不需要人为关注的组件转换为Windows服务,Windows服务在使用的过程中有很多好处,相信这一点,就不用我多说了。但是每次都要建立Windows服务项目,编写服务代码,建立服务的安装程序,然后还要通过InstallUtil.exe这个命令来安装Windows服务,如果要是想卸载也要使用这个命令,只是在InstallUtil.exe这个命令后增加一个参数/u,表示卸载,我相信大家都对这个很熟悉了,我就不多说了。
我为了不想使用这个命令来安装和卸载Windows服务,我就自己写了一个工具类,已经完全经过了单元测试,完全靠谱,大家可以放心使用。话不多说,直接上代码,代码有注释,其实也不是很难,相信大家都能看的懂。
1 using System; 2 System.Collections; 3 System.Collections.Generic; 4 System.Configuration.Install; 5 System.IO; 6 System.Linq; 7 System.ServiceProcess; 8 System.Text; 9 System.Threading.Tasks; 10 11 namespace Enterprise.Framework.Utils 12 { 13 /// <summary> 14 /// Windows服务实例的管理器,可以安装、启动、关停和卸载Windows服务实例 15 </summary> 16 public static class WindowsServiceInstanceManager 17 { 18 19 判断指定的名称的Windows服务是否存在 20 21 <param name="serviceName">Windows服务的名称</param> 22 <returns>返回布尔值,true表示指定名称的Windows服务存在,false表示指定名称的Windows服务不存在</returns> 23 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 false 34 } 35 36 37 安装Windows服务,如果存在同名的服务,也认为安装时成功的 38 39 <param name="serviceFilePath">要安装的Windows服务的文件的地址 40 要安装的Windows服务的名称,可以根据服务的名称判断其服务是否存在,如果服务存在,就不需要安装 41 返回布尔值,true表示安装Windows服务成功,false表示安装Windows服务失败 42 bool InstallService(string serviceFilePath,1)"> 43 44 string.IsNullOrEmpty(serviceFilePath) || .IsNullOrWhiteSpace(serviceFilePath)) 45 46 47 48 if (!File.Exists(serviceFilePath)) 49 50 51 52 this.IsServiceExisted(serviceName)) 53 54 55 56 using (AssemblyInstaller installer = new AssemblyInstaller()) 57 58 try 59 60 installer.UseNewContext = 61 installer.Path = serviceFilePath; 62 IDictionary savedState = Hashtable(); 63 installer.Install(savedState); 64 installer.Commit(savedState); 65 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 bool UninstallService( 81 82 83 84 85 86 87 88 89 90 IsServiceExisted(serviceName)) 91 92 93 94 95 96 97 98 installer.UseNewContext = 99 installer.Path =100 installer.Uninstall(null); 101 102 103 104 105 106 107 108 109 110 111 启动Windows服务 112 113 要启动的Windows服务的名称114 返回布尔值,true表示启动Windows服务成功,false表示启动Windows服务失败115 bool StartService(116 117 string.IsNullOrEmpty(serviceName) || .IsNullOrWhiteSpace(serviceName)) 118 119 120 121 using (ServiceController control = ServiceController(serviceName)) 122 123 124 125 if (control.Status == ServiceControllerStatus.Stopped) 126 { 127 control.Start(); 128 } 129 130 131 132 133 134 135 136 137 138 139 关停Windows服务 140 141 要关停Windows服务的名称142 返回布尔值,true表示关停Windows服务成功,false表示关停Windows服务失败143 bool StopService(144 145 146 147 148 149 150 151 152 153 ServiceControllerStatus.Running) 154 155 control.Stop(); 156 157 158 159 160 161 162 163 164 165 } 166 }
好了,这就是今天自己的一点小作品,每天进步一点点,努力坚持。不忘初心,继续努力吧,欢迎大家前来讨论。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。