Use .NET To See If Windows Service Is Running

By Robbe Morris

Quick code sample to query the management objects and see if a specific windows service is running.

using System.Management;

public
static bool IsServiceRunning(string serviceEXEName)
{
  var mos = new ManagementObjectSearcher();
        mos.Query = new ObjectQuery("SELECT * FROM win32_process");

    serviceEXEName = serviceEXEName.
ToLower().Trim();

    foreach (ManagementObject mo in mos.Get())
    {
      if (serviceEXEName == mo["Name"].ToString().ToLower()) return true;
    }

    return false;
}

Use .NET To See If Windows Service Is Running  (926 Views)