C# code to check status of a windows service
Below is the C# code which return the status of a windows service running on a machine/server on passing correct name.
One use case of it is that if we want to monitor some specific services which are critical to operation, we can schedule our application to check the status of the specific service/services and if it's ha stopped, we can do certain action like sending an email to concern people so that corrective action can be taken.
Include using System.ServiceProcess; reference in your application.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static String GetWindowsServiceStatus(String SERVICENAME)
{
ServiceController sc = new ServiceController(SERVICENAME);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Below is the sample test result:
Hope it would be helpful..!!
Really wonderful blog! It is great to see such fresh contents once in a while. Do write more such articles. Regards.
ReplyDeleteMicrosoft Dynamics CRM Training in Chennai
Microsoft Dynamics Training in Chennai
Spark Training in Chennai
Spark Training Academy Chennai
Unix Training in Chennai
Unix Shell Scripting Training in Chennai
Microsoft Dynamics CRM Training in Tambaram
Microsoft Dynamics CRM Training in Adyar
I eagarly reading all the content.Thanks for posting the best idea. keep update the content just like this.
ReplyDeleteData Extraction Solutions
payment gateway companies
CRM Services
Thanks for sharing such an excellent idea. I think it is worth trying to implement it for my CRM system.
ReplyDelete