2012-06-16 12 views

Odpowiedz

54

Dodaj odwołanie do System.ServiceProcess.dll. Następnie możesz użyć klasy ServiceController.

+0

nie rozpoznaje tego przy użyciu System.ServiceProcess; - używam .net 4 – AlexandruC

+0

@lxClan dodaj odniesienie do twojego projektu – Zbigniew

+0

Zaktualizowałem odpowiedź z referencją, którą musisz dodać. –

2

Można zrobić to w ten sposób, Details of Service Controller

ServiceController sc = new ServiceController("your service name"); 
if (sc.Status == ServiceControllerStatus.Stopped) 
{ 
    sc.Start(); 

} 

Podobnie można zatrzymać za pomocą metody zatrzymania

sc.Stop(); 
+0

go nie rozpoznaje, używając System.ServiceProcess; - używam .net 4 – AlexandruC

+1

Dodaj przestrzeń nazw, prawdopodobnie brakuje jej. – edocetirwi

15

Najpierw dodaj odniesienie do zestawu System.ServiceProcess.

Aby rozpocząć:

ServiceController service = new ServiceController("YourServiceName"); 
service.Start(); 
var timeout = new TimeSpan(0, 0, 5); // 5seconds 
service.WaitForStatus(ServiceControllerStatus.Running, timeout); 

Aby zatrzymać:

ServiceController service = new ServiceController("YourServiceName"); 
service.Stop(); 
var timeout = new TimeSpan(0, 0, 5); // 5seconds 
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); 

Oba przykłady pokazują, jak czekać, aż usługa osiągnęła nowy status (bieganie, zatrzymał się ... itd.). Parametr limitu czasu w WaitForStatus jest opcjonalny.

+0

nie rozpoznaje tego przy użyciu System.ServiceProcess; - używam .net 4 – AlexandruC

+0

Powinien działać dobrze, ale musisz dodać odniesienie do System.ServiceProcess. –

+0

Dobrze! głupi ja. dzięki. wyraźny! – AlexandruC

0

jest brudniejsze, ale same same ..
tylko wykonywać polecenia powłoki

NET STOP "MYSERVICENAME" 
NET START "MYSERVICENAME" 
0
// Check whether the U-TEST RTC service is started. 
     ServiceController sc = new ServiceController(); 
     sc.ServiceName = "U-TEST RTC"; 
     m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStatusService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 

     if (sc.Status == ServiceControllerStatus.Stopped) 
     { 
      m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      try 
      { 
       // Start the service, and wait until its status is "Running". 
       sc.Start(); 
       var timeout = new TimeSpan(0, 0, 5); // 5seconds 
       sc.WaitForStatus(ServiceControllerStatus.Running, timeout); 
       m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgNowService"), sc.Status.ToString()), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      } 
      catch (InvalidOperationException) 
      { 
       m_objMainChainRTC.m_objUC.ValidationLogMessages(String.Format(LocalizeDictionary.Instance.GetLocalizedValue("MsgExceptionStartService")), Alstom.Automation.Forms.ViewModels.RTCAutomationViewModel.ColorLog.Log); 
      } 
     }