Code Example : Checking Windows Service Status in C#

Sometimes it is useful to be able to check for the existence of a windows service on a local or remote machine and also check the running status of that service. Doing this is very easy in .NET. The following snippets are taken from the windows services tests in the Post Deployment Smoke Tester.

var ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == "MyService");
if (ctl == null) throw new AssertionException(string.Format("Service with name [{0}] was not found", "MyService"));

The snippet above will check for a named service on the local machine. You can also look for the existence of a service on a remote machine, but supplying the machine name in the call to GetServices, as shown below.

var ctl = ServiceController.GetServices("CENTLPBZBHV1").FirstOrDefault(s => s.ServiceName == "MyService");
if (ctl == null) throw new AssertionException(string.Format("Service with name [{0}] was not found on {1}", "MyService", "CENTLPBZBHV1"));

You can find out the service name using the services.msc application in Windows. If we use the “DNS Client” as an example, you will see it has a name in the screen shot below. This isn’t the name you need to use when querying for the service in your C# code. To get the actual name you need to right click on the service and select properties.

Windows Services list

Windows Services list

When the dialog box shows up, you will see there is a field at the top called Service Name. This is the one you need. In this example, the actual service name we need for the code is “Dnscache

Windows Service Properties

Windows Service Properties

Once you have determined that a service in installed you often need to check the status of that service. There are many service statuses that are defined by the .NET Frameworks ServiceControllerStatus class. These are defined as follows:

    // Summary:
    //     Indicates the current state of the service.
    public enum ServiceControllerStatus
    {
        // Summary:
        //     The service is not running. This corresponds to the Win32 SERVICE_STOPPED
        //     constant, which is defined as 0x00000001.
        Stopped = 1,
        //
        // Summary:
        //     The service is starting. This corresponds to the Win32 SERVICE_START_PENDING
        //     constant, which is defined as 0x00000002.
        StartPending = 2,
        //
        // Summary:
        //     The service is stopping. This corresponds to the Win32 SERVICE_STOP_PENDING
        //     constant, which is defined as 0x00000003.
        StopPending = 3,
        //
        // Summary:
        //     The service is running. This corresponds to the Win32 SERVICE_RUNNING constant,
        //     which is defined as 0x00000004.
        Running = 4,
        //
        // Summary:
        //     The service continue is pending. This corresponds to the Win32 SERVICE_CONTINUE_PENDING
        //     constant, which is defined as 0x00000005.
        ContinuePending = 5,
        //
        // Summary:
        //     The service pause is pending. This corresponds to the Win32 SERVICE_PAUSE_PENDING
        //     constant, which is defined as 0x00000006.
        PausePending = 6,
        //
        // Summary:
        //     The service is paused. This corresponds to the Win32 SERVICE_PAUSED constant,
        //     which is defined as 0x00000007.
        Paused = 7,
    }

To query the service status you do the following :

var ctl = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == ServiceName);
AssertState.Equal(ServiceControllerStatus.Running, ctl.Status);

Again, if you want to check the status of a windows service on a remote machine, you specify the machine name in the call to GetServices.

var ctl = ServiceController.GetServices("CENTLPBZBHV1").FirstOrDefault(s => s.ServiceName == ServiceName);
AssertState.Equal(ServiceControllerStatus.Running, ctl.Status);

That concludes this short article on querying windows services. As you can see, it is a simple thing to do from C#, but it is also a very useful set of steps to be aware off. Again, if you need to do this in a post deployment testing environment, then these test types are built into the Post Deployment Smoke Tester.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s