【C#】關於服務Service的操作(是否存在,是否啟動,是否自動啟動)
阿新 • • 發佈:2019-01-27
轉載於http://blog.csdn.net/smartsmile2012/article/details/8666635
#region Windows服務控制區 /// <summary> /// 1,在使用ServiceController類前,我們必須引用名稱空間System.ServiceProcess /// ,而引用該名稱空間,我們又必須在引用中新增System.ServiceProcess引用 /// /// 2,ServiceController有兩個名稱,一個是DisplayName,一個是ServiceName: /// DisplayName-服務的友好名稱,可用於標識該服務(就是我們在"控制面板"- >"服務"中看到的顯示名稱) /// ServiceName--服務進行標識的名稱(這個才是服務的真正名稱,也就是我們在"控制面板"- >"服務"中看到的服務名稱) /// /// 3,ServiceController中其它一些有用的方法與屬性: /// 1)Status屬性:指示該服務現在是正在執行、已停止還是已暫停,或者啟動、停止、暫停或繼續命令被掛起。 /// 2)Pause()方法:暫停該服務。 /// 3)Start()方法:啟動該服務。 /// 4)Stop()方法:停止該服務。 /// </summary>
#region 安裝服務 /// <summary> /// 安裝服務 /// </summary> private bool InstallService(string NameService) { bool flag = true; if (!IsServiceIsExisted(NameService)) { try { string location = System.Reflection.Assembly.GetExecutingAssembly().Location; string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe"; InstallmyService(null, serviceFileName); } catch { flag = false; } } return flag; } #endregion #region 解除安裝服務 /// <summary> /// 解除安裝服務 /// </summary> private bool UninstallService(string NameService) { bool flag = true; if (IsServiceIsExisted(NameService)) { try { string location = System.Reflection.Assembly.GetExecutingAssembly().Location; string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe"; UnInstallmyService(serviceFileName); } catch { flag = false; } } return flag; } #endregion #region 檢查服務存在的存在性 /// <summary> /// 檢查服務存在的存在性 /// </summary> /// <param name=" NameService ">服務名</param> /// <returns>存在返回 true,否則返回 false;</returns> public static bool IsServiceIsExisted(string NameService) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName.ToLower() == NameService.ToLower()) { return true; } } return false; } #endregion #region 安裝Windows服務 /// <summary> /// 安裝Windows服務 /// </summary> /// <param name="stateSaver">集合</param> /// <param name="filepath">程式檔案路徑</param> public static void InstallmyService(IDictionary stateSaver, string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Install(stateSaver); AssemblyInstaller1.Commit(stateSaver); AssemblyInstaller1.Dispose(); } #endregion #region 解除安裝Windows服務 /// <summary> /// 解除安裝Windows服務 /// </summary> /// <param name="filepath">程式檔案路徑</param> public static void UnInstallmyService(string filepath) { AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller(); AssemblyInstaller1.UseNewContext = true; AssemblyInstaller1.Path = filepath; AssemblyInstaller1.Uninstall(null); AssemblyInstaller1.Dispose(); } #endregion #region 判斷window服務是否啟動 /// <summary> /// 判斷某個Windows服務是否啟動 /// </summary> /// <returns></returns> public static bool IsServiceStart(string serviceName) { ServiceController psc = new ServiceController(serviceName); bool bStartStatus = false; try { if (!psc.Status.Equals(ServiceControllerStatus.Stopped)) { bStartStatus = true; } return bStartStatus; } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion #region 修改服務的啟動項 /// <summary> /// 修改服務的啟動項 2為自動,3為手動 /// </summary> /// <param name="startType"></param> /// <param name="serviceName"></param> /// <returns></returns> public static bool ChangeServiceStartType(int startType, string serviceName) { try { RegistryKey regist = Registry.LocalMachine; RegistryKey sysReg = regist.OpenSubKey("SYSTEM"); RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet"); RegistryKey services = currentControlSet.OpenSubKey("Services"); RegistryKey servicesName = services.OpenSubKey(serviceName, true); servicesName.SetValue("Start", startType); } catch (Exception ex) { return false; } return true; } #endregion #region 啟動服務 private bool StartService(string serviceName) { bool flag = true; if (IsServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); for (int i = 0; i < 60; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { break; } if (i == 59) { flag = false; } } } } return flag; } #endregion #region 停止服務 private bool StopService(string serviceName) { bool flag = true; if (IsServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { service.Stop(); for (int i = 0; i < 60; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { break; } if (i == 59) { flag = false; } } } } return flag; } #endregion #endregion