最近在重構公司的系統,把一些需要獨立執行、並不需要人為關注的組件轉換為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 }
好了,這就是今天自己的一點小作品,每天進步一點點,努力堅持。不忘初心,繼續努力吧,歡迎大家前來討論。