Retrieve The Status of a Windows Service

Here is an easy way to get the status of a Windows service.

''' <summary>
''' Gets the status of a Windows service.
''' </summary>
''' <param name="serviceName">Name of the service.</param>
''' <returns>ServiceControllerStatus.</returns>
''' <exception cref="InvalidOperationException">Service not 
''' <exception cref="System.InvalidOperationException">Service 
               not found.</exception>
Public Function ServiceStatus(serviceName As StringAs ServiceControllerStatus
    Dim service As ServiceController = LoadService(serviceName)
    If service IsNot Nothing Then
        Return service.Status
        Throw New InvalidOperationException("Service not 
    End If
End Function
''' <summary>
''' Loads the service.
''' </summary>
''' <param name="serviceName">Name of the service.</param>
''' <returns>ServiceController.</returns>
Private Function LoadService(serviceName As StringAs 
    Return ServiceController.GetServices().Where(Function(p) 
           p.ServiceName = serviceName).FirstOrDefault()
End Function

Here are the values for service status:

  • ContinuePending
  • Paused
  • PausePending
  • Running
  • StartPending
  • Stopped
  • StopPending

This and lost  more code can be found in the dotNetTips.Utility open source project.


Leave a Reply

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

You are commenting using your 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