This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Programming
.NET 1.1+

Detect the Status of a Windows Service

When developing software that relies upon the availability of Windows services, it is important to be able to determine a service's status. This article explains how to check if a service is running, either on a local computer or on a remote server.

ServiceController Class

The ServiceController class is available within the System.ServiceProcess namespace of the .NET framework. It represents a Windows service, either on the local machine or on a remote server, and allows that service to be examined or manipulated. In this article we will use the ServiceController class to determine if local and remote services are currently operational.

Accessing the ServiceController Class

The DLL containing the ServiceProcess namespace is not referenced by Visual Studio by default for new Windows Forms projects. To enable use of the ServiceController class, a reference to System.ServiceProcess.dll must be added to the project.

To use the sample code below, you must also add the following using directive at the top of your code file:

using System.ServiceProcess;

Detecting the Status of a Local Service

To determine whether a local service is running, a ServiceController object must be created that references the service. The service may be referred to by name, by setting the ServiceName property, or by its display name, by setting the DisplayName property. The display name of a service is that which may be seen in the service list from the Windows Control Panel. The service name is the shorter version that can be found in the service's properties.

The properties can be set directly or by using a constructor. The constructor option is the simplest, accepting a single string containing either the service name or display name. However, if these names are ambiguous the properties must be set directly. Once one property is correctly set, the other name is updated automatically to match that of the service.

The three samples below each create a ServiceController object linked to the Print Spooler service.

// Link by service name
ServiceController byServiceName = new ServiceController();
byServiceName.ServiceName = "Spooler";

// Link by display name
ServiceController byDisplayName = new ServiceController();
byDisplayName.ServiceName = "Print Spooler";

// Link by constructor
ServiceController byConstructor = new ServiceController("Spooler");

Once the service has been referenced in one of the manners described above, the status of the service can be read from the ServiceController's Status property. The property returns a value from the ServiceControllerStatus enumeration. This enumerated type has seven constant values that relate to the seven possible statuses of any service. The values are as follows:

ContinuePendingThe service has been paused and is about to continue.
PausedThe service is paused.
PausePendingThe service is in the process of pausing.
RunningThe service is running.
StartPendingThe service is in the process of starting.
StoppedThe service is not running.
StopPendingThe service is in the process of stopping.

To check if a service is running, the following code can be used. This sample uses the Print Spooler service. Try running the code with the service in various states to see the results.

ServiceController sc = new ServiceController("Spooler");

if (sc.Status == ServiceControllerStatus.Running)
    MessageBox.Show("The service is running.");

NB: Full trust is required to read a service's status. If the user does not have full trust rights an exception is thrown.

Detecting the Status of a Remote Service

The status of a service on a remote server can be determined almost as simply as that of a local service. One additional property must be set. This is the MachineName property, which simply holds the name of the computer upon which the service to be checked resides.

The MachineName property can be set directly or by using a second, overloaded version of the constructor. The second version requires two string parameters. The first specifies the name of the service and the second the name of the machine. This property is not required to be set for local services as it defaults to ".", representing the local machine.

The following code is similar to the previous example. In this case, the program checks the status of the Print Spooler on a remote machine named "Server1":

ServiceController sc = new ServiceController("Spooler", "Server1");

if (sc.Status == ServiceControllerStatus.Running)
    MessageBox.Show("The service is running.");
27 May 2008