2012-06-16 18 views

risposta

54

Aggiungere un riferimento al System.ServiceProcess.dll. Quindi è possibile utilizzare la classe ServiceController.

// Check whether the Alerter service is started. 
ServiceController sc = new ServiceController(); 
sc.ServiceName = "Alerter"; 
Console.WriteLine("The Alerter service status is currently set to {0}", 
        sc.Status.ToString()); 

if (sc.Status == ServiceControllerStatus.Stopped) 
{ 
    // Start the service if the current status is stopped. 
    Console.WriteLine("Starting the Alerter service..."); 
    try 
    { 
     // Start the service, and wait until its status is "Running". 
     sc.Start(); 
     sc.WaitForStatus(ServiceControllerStatus.Running); 

     // Display the current service status. 
     Console.WriteLine("The Alerter service status is now set to {0}.", 
         sc.Status.ToString()); 
    } 
    catch (InvalidOperationException) 
    { 
     Console.WriteLine("Could not start the Alerter service."); 
    } 
} 
+0

non riconosce questo utilizzando System.ServiceProcess; - sto usando .net 4 – AlexandruC

+0

@lxClan aggiungi riferimento nel tuo progetto – Zbigniew

+0

Ho aggiornato la risposta con il riferimento che devi aggiungere. –

2

Si può fare in questo modo, Details of Service Controller

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

} 

Allo stesso modo si può smettere di usare il metodo di arresto

sc.Stop(); 
+0

non riconosce questo using System.ServiceProcess; - sto usando .net 4 – AlexandruC

+1

Aggiungi lo spazio dei nomi, probabilmente ti manca. – edocetirwi

15

Prima di tutto aggiungere un riferimento all'assembly System.ServiceProcess.

Per iniziare:

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

Per fermare:

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

Entrambi gli esempi mostrano come aspettare fino a quando il servizio ha raggiunto un nuovo status (in esecuzione, fermato ... ecc.). Il parametro di timeout in WaitForStatus è facoltativo.

+0

non lo riconosce utilizzando System.ServiceProcess; - sto usando .net 4 – AlexandruC

+0

Dovrebbe funzionare bene, ma devi aggiungere un riferimento a System.ServiceProcess. –

+0

Giusto! sciocco me. Grazie. segnato! – AlexandruC

0

c'è un sporco, ma lo stesso stesso ..
basta eseguire il comando shell

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); 
      } 
     } 
Problemi correlati