Service control : Service : Windows C# Examples


C# Examples » Windows » Service »

 

Service control









    
using  System;
using  System.ServiceProcess;

class  MainClass
{
    public  static  void  Main()  
    {
        ServiceController  scAlerter  =  new  ServiceController("Alerter");

        Console.WriteLine(scAlerter.DisplayName);
        Console.WriteLine(scAlerter.CanStop);

        scAlerter.Stop();
        Console.WriteLine("Service  stopped");
        scAlerter.Start();
        Console.WriteLine("Service  started");
    }
}
    
   
  
   



Output

Alerter
False

Unhandled Exception: System.InvalidOperationException: Cannot stop Alerter service on computer '.'.
---> System.ComponentModel.Win32Exception: The service has not been started
   --- End of inner exception stack trace ---
   at System.ServiceProcess.ServiceController.Stop()
   at MainClass.Main()


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Windows
» Service