1 using System; 2 using System.Collections; 3 using System.Configuration.Install; 4 using System.Linq; 5 using System.ServiceProcess; 6 7 namespace AutoInstallUtil 8 { 9 public class SystemServices 10 { 11 /// <summary> 12 /// 打開系統服務 13 /// </summary> 14 /// <param name="serviceName">系統服務名稱</param> 15 /// <returns></returns> 16 public static bool SystemServiceOpen(string serviceName) 17 { 18 try 19 { 20 using (var control = new ServiceController(serviceName)) 21 { 22 if (control.Status != ServiceControllerStatus.Running) 23 { 24 control.Start(); 25 } 26 } 27 return true; 28 } 29 catch 30 { 31 return false; 32 } 33 } 34 35 36 /// <summary> 37 /// 關閉系統服務 38 /// </summary> 39 /// <param name="serviceName">系統服務名稱</param> 40 /// <returns></returns> 41 public static bool SystemServiceClose(string serviceName) 42 { 43 try 44 { 45 using (var control = new ServiceController(serviceName)) 46 { 47 48 if (control.Status == ServiceControllerStatus.Running) 49 { 50 control.Stop(); 51 } 52 } 53 return true; 54 } 55 catch 56 { 57 return false; 58 } 59 } 60 61 /// <summary> 62 /// 重啟系統服務 63 /// </summary> 64 /// <param name="serviceName">系統服務名稱</param> 65 /// <returns></returns> 66 public static bool SystemServiceReStart(string serviceName) 67 { 68 try 69 { 70 using (var control = new ServiceController(serviceName)) 71 { 72 if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running) 73 { 74 control.Continue(); 75 } 76 } 77 return true; 78 } 79 catch 80 { 81 return false; 82 } 83 } 84 85 /// <summary> 86 /// 返回服務狀態 87 /// </summary> 88 /// <param name="serviceName">系統服務名稱</param> 89 /// <returns>1:服務未運行 2:服務正在啟動 3:服務正在停止 4:服務正在運行 5:服務即將繼續 6:服務即將暫停 7:服務已暫停 0:未知狀態</returns> 90 public static int GetSystemServiceStatus(string serviceName) 91 { 92 try 93 { 94 using (var control = new ServiceController(serviceName)) 95 { 96 return (int)control.Status; 97 } 98 } 99 catch 100 { 101 return 0; 102 } 103 } 104 105 /// <summary> 106 /// 返回服務狀態 107 /// </summary> 108 /// <param name="serviceName">系統服務名稱</param> 109 /// <returns>1:服務未運行 2:服務正在啟動 3:服務正在停止 4:服務正在運行 5:服務即將繼續 6:服務即將暫停 7:服務已暫停 0:未知狀態</returns> 110 public static string GetSystemServiceStatusString(string serviceName) 111 { 112 try 113 { 114 using (var control = new ServiceController(serviceName)) 115 { 116 var status = string.Empty; 117 switch ((int)control.Status) 118 { 119 case 1: 120 status = "服務未運行"; 121 break; 122 case 2: 123 status = "服務正在啟動"; 124 break; 125 case 3: 126 status = "服務正在停止"; 127 break; 128 case 4: 129 status = "服務正在運行"; 130 break; 131 case 5: 132 status = "服務即將繼續"; 133 break; 134 case 6: 135 status = "服務即將暫停"; 136 break; 137 case 7: 138 status = "服務已暫停"; 139 break; 140 case 0: 141 status = "未知狀態"; 142 break; 143 } 144 return status; 145 } 146 } 147 catch 148 { 149 return "未知狀態"; 150 } 151 } 152 153 /// <summary> 154 /// 安裝服務 155 /// </summary> 156 /// <param name="stateSaver"></param> 157 /// <param name="filepath"></param> 158 public static void InstallService(IDictionary stateSaver, string filepath) 159 { 160 try 161 { 162 var myAssemblyInstaller = new AssemblyInstaller 163 { 164 UseNewContext = true, 165 Path = filepath 166 }; 167 myAssemblyInstaller.Install(stateSaver); 168 myAssemblyInstaller.Commit(stateSaver); 169 myAssemblyInstaller.Dispose(); 170 } 171 catch (Exception ex) 172 { 173 throw new Exception("installServiceError/n" + ex.Message); 174 } 175 } 176 177 public static bool ServiceIsExisted(string serviceName) 178 { 179 ServiceController[] services = ServiceController.GetServices(); 180 return services.Any(s => s.ServiceName == serviceName); 181 } 182 183 /// <summary> 184 /// 卸載服務 185 /// </summary> 186 /// <param name="filepath">路徑和文件名</param> 187 public static void UnInstallService(string filepath) 188 { 189 try 190 { 191 //UnInstall Service 192 var myAssemblyInstaller = new AssemblyInstaller 193 { 194 UseNewContext = true, 195 Path = filepath 196 }; 197 myAssemblyInstaller.Uninstall(null); 198 myAssemblyInstaller.Dispose(); 199 } 200 catch (Exception ex) 201 { 202 throw new Exception("unInstallServiceError/n" + ex.Message); 203 } 204 } 205 } 206 }
接下來我們封裝控制台的操作方法為了實現循環監聽這裡用了遞歸
1 using System; 2 using System.Diagnostics; 3 using System.IO; 4 using System.Linq; 5 6 namespace AutoInstallUtil 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 try 13 { 14 ServerAction(); 15 } 16 catch (Exception ex) 17 { 18 Console.WriteLine("發生錯誤:{0}", ex.Message); 19 } 20 21 Console.ReadKey(); 22 } 23 24 /// <summary> 25 /// 操作 26 /// </summary> 27 private static void ServerAction() 28 { 29 Console.WriteLine("請輸入:1安裝 2卸載"); 30 var condition = Console.ReadLine(); 31 var currentPath = Environment.CurrentDirectory; 32 var currentFileNameVshost = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName).ToLower(); 33 var currentFileName = currentFileNameVshost.Replace(".vshost.exe", ".exe"); 34 var files = 35 Directory.GetFiles(currentPath) 36 .Select(o => Path.GetFileName(o).ToLower()) 37 .ToList() 38 .Where( 39 o => 40 o != currentFileNameVshost 41 && o != currentFileName 42 && o.ToLower().EndsWith(".exe") 43 && o != "installutil.exe" 44 && !o.ToLower().EndsWith(".vshost.exe")) 45 .ToList(); 46 if (files.Count == 0) 47 { 48 Console.WriteLine("未找到可執行文件,請確認當前目錄有需要安裝的服務程序"); 49 } 50 else 51 { 52 Console.WriteLine("找到目錄有如下可執行文件,請選擇需要安裝或卸載的文件序號:"); 53 } 54 int i = 0; 55 foreach (var file in files) 56 { 57 Console.WriteLine("序號:{0} 文件名:{1}", i, file); 58 i++; 59 } 60 var serviceFileIndex = Console.ReadLine(); 61 var servicePathName = currentPath + "\\" + files[Convert.ToInt32(serviceFileIndex)]; 62 if (condition == "1") 63 { 64 SystemServices.InstallService(null, servicePathName); 65 } 66 else 67 { 68 SystemServices.UnInstallService(servicePathName); 69 } 70 Console.WriteLine("**********本次操作完畢**********"); 71 ServerAction(); 72 } 73 }