本文實例講述了C#實現操作windows系統服務(service)的方法。分享給大家供大家參考。具體如下:
這段C#代碼可以控制啟動和停止系統服務
/// <summary>/// 停止服務/// </summary>private bool StopService(string StopServiceName){  ServiceController service = new ServiceController(StopServiceName);  try  {   service.Stop();   service.WaitForStatus(ServiceControllerStatus.Stopped);  }  catch(Exception)  {  return false; } return true;}/// <summary>/// 開啟服務/// </summary>private bool StartService(string StartServiceName) {  ServiceController service = new ServiceController(StartServiceName);  try  {    service.Start();    service.WaitForStatus(ServiceControllerStatus.Running); }  catch (Exception) {  return false; } return true;}希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答