Wednesday, December 24, 2014

SERVICES.MSC get service status using C#


Description:

Following code can be used to get service (services.msc) status using C#:

static string checkservice(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";
    }
}

No comments:

Post a Comment