場景 使用C#編寫的Windows服務程式,在Winform中進行調用。 常用工具類方法檢測服務是否存在或者安裝,獲取服務狀態,啟動服務,停止服務的方法。 以在Winform中重啟服務為例。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公 ...
場景
使用C#編寫的Windows服務程式,在Winform中進行調用。
常用工具類方法檢測服務是否存在或者安裝,獲取服務狀態,啟動服務,停止服務的方法。
以在Winform中重啟服務為例。
註:
博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關註公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載
實現
新建工具類WinServiceHelper
檢測服務是否安裝或者存在的方法
/// <summary> /// 服務是否安裝/存在 /// </summary> /// <param name="serviceName">服務名</param> /// <returns></returns> public static bool IsServiceInstalled(string serviceName) { bool exists = false; System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController s in services) { if (s.ServiceName == serviceName) { exists = true; break; } } return exists; }
獲取服務狀態的方法
/// <summary> /// 獲取服務狀態 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> public static String GetServiceStatus(string serviceName) { string result = "服務不存在"; System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController s in services) { if (s.ServiceName == serviceName) { result = s.Status.ToString(); break; } } return result; }
註:
服務狀態返回值是枚舉類型,具體返回值如下
// 摘要: // 指示服務的當前狀態。 public enum ServiceControllerStatus { // 摘要: // 服務未運行。這對應於 Win32 SERVICE_STOPPED 常數,該常數定義為 0x00000001。 Stopped = 1, // // 摘要: // 服務正在啟動。這對應於 Win32 SERVICE_START_PENDING 常數,該常數定義為 0x00000002。 StartPending = 2, // // 摘要: // 服務正在停止。這對應於 Win32 SERVICE_STOP_PENDING 常數,該常數定義為 0x00000003。 StopPending = 3, // // 摘要: // 服務正在運行。這對應於 Win32 SERVICE_RUNNING 常數,該常數定義為 0x00000004。 Running = 4, // // 摘要: // 服務即將繼續。這對應於 Win32 SERVICE_CONTINUE_PENDING 常數,該常數定義為 0x00000005。 ContinuePending = 5, // // 摘要: // 服務即將暫停。這對應於 Win32 SERVICE_PAUSE_PENDING 常數,該常數定義為 0x00000006。 PausePending = 6, // // 摘要: // 服務已暫停。這對應於 Win32 SERVICE_PAUSED 常數,該常數定義為 0x00000007。 Paused = 7, }
啟動服務的方法
/// <summary> /// 啟動服務 /// </summary> /// <param name="serivceExeFullPath">服務全路徑</param> /// <param name="serviceName">服務名</param> /// <returns></returns> public static bool ServiceStart(string serivceExeFullPath ,string serviceName) { if (!IsServiceInstalled(serviceName)) { MessageBox.Show("服務未安裝,請先安裝!"); return false; } try { using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = serivceExeFullPath; p.StartInfo.Arguments = "start"; p.Start(); p.Close(); } System.Threading.Thread.Sleep(2000); return true; } catch (Exception ex) { MessageBox.Show("服務安裝異常:" + ex.Message); return false; } }
停止服務的方法
/// <summary> /// 停止服務 /// </summary> /// <param name="serivceExeFullPath">服務全路徑</param> /// <param name="serviceName">服務名</param> /// <returns></returns> public static bool ServiceStop(string serivceExeFullPath, string serviceName) { if (!IsServiceInstalled(serviceName)) { MessageBox.Show("服務未安裝,請先安裝!"); return false; } try { using (System.Diagnostics.Process p = new System.Diagnostics.Process()) { p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = serivceExeFullPath; p.StartInfo.Arguments = "stop"; p.Start(); p.WaitForExit(); p.Close(); } System.Threading.Thread.Sleep(2000); return true; } catch (Exception ex) { MessageBox.Show("服務停止異常:" + ex.Message); return false; } }
重啟服務示例
在重啟服務的按鈕的點擊事件中
//檢測服務是否安裝 bool isInstalled = WinServiceHelper.IsServiceInstalled(Global.BTS_DATA_SERVICE_NAME); if (!isInstalled) { MessageBox.Show("重啟失敗,服務"+Global.BTS_DATA_SERVICE_NAME+"未安裝或未啟動"); return; } string serviceStatus = WinServiceHelper.GetServiceStatus(Global.BTS_DATA_SERVICE_NAME); if (!serviceStatus.Equals(System.ServiceProcess.ServiceControllerStatus.Running.ToString())) { MessageBox.Show("重啟失敗,服務" + Global.BTS_DATA_SERVICE_NAME + "狀態為:" + serviceStatus); return; } string serivceExeFullPath = Global.AppConfig.BtsDataServiceExe; string serviceName = Global.BTS_DATA_SERVICE_NAME; bool isStopSuccess = WinServiceHelper.ServiceStop(serivceExeFullPath,serviceName); //停止失敗 if (!isStopSuccess) { MessageBox.Show("重啟失敗,服務" + Global.BTS_DATA_SERVICE_NAME + "停止失敗"); return; } //方法里已經休眠2秒 bool isStartSuccess = WinServiceHelper.ServiceStart(serivceExeFullPath, serviceName); //啟動失敗 if (!isStartSuccess) { MessageBox.Show("重啟失敗,服務" + Global.BTS_DATA_SERVICE_NAME + "啟動失敗"); return; } MessageBox.Show("服務" + Global.BTS_DATA_SERVICE_NAME + "重啟成功");