Retrieve The Status of a Windows Service

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

Public Function ServiceStatus(serviceName As String) As 
  ServiceControllerStatus
    Dim service As ServiceController = LoadService(serviceName)
    If service IsNot Nothing Then
        Return service.Status
    Else
        Throw New InvalidOperationException("Service not  found.")
    End If
End Function
Private Function LoadService(serviceName As String) As 
  ServiceController     
    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.