C# : Monitoring the windows services

This example prints the status of windows service in local machine and also says How to start and stop windows services.

The following namespace required:

using System.ServiceProcess;
//Instantiate the ServiceController object

System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("Service_name");

Console.WriteLine(sc.Status);

//You can verify the status if stopped or not like below

if
(sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
 {       System.Console.WriteLine();       System.Console.WriteLine("Service_name service is stopped");       return;   }

// Stop the service
sc.Stop();

// Start the service
sc.Start();

By Perry    Popularity  (3180 Views)