[csharp] #region Windows服務控制區 #region 安裝服務 private void InstallService(IDictionary stateSaver, string filepath) { try { ServiceController service = new ServiceController("ServiceName"); if (!ServiceIsExisted("ServiceName")) { //Install Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Install(stateSaver); myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose(); //--Start Service service.Start(); } else { if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); } } } catch (Exception ex) { MessageBox.Show("安裝服務失敗!"); } } #endregion #region 卸載windows服務 private void UnInstallService(string filepath) { try { if (ServiceIsExisted("ServiceName")) { //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex) { MessageBox.Show("卸載服務失敗!"); } } #endregion #region 判斷window服務是否存在 private bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } #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 啟動服務 private bool StartService(string serviceName) { bool flag = true; if (ServiceIsExisted(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 (ServiceIsExisted(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++) { www.2cto.com service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { break; } if (i == 59) { flag = false; } } } } return flag; } #endregion #endregion