C# .NET - How can we check if specific windows service exists in machine

Asked By Sachin Mishra on 23-Apr-09 11:24 AM

I am working in application in which i want to check if specific windows service exists in machine or not.

Please suggest the code.

yes it easy - Ravenet Rasaiyah replied to Sachin Mishra on 23-Apr-09 11:46 AM

Hi


ServiceController[] services = ServiceController.GetServices();

// try to find service name
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
return true;
}
return false;

thank you
Santhosh N replied to Sachin Mishra on 23-Apr-09 12:02 PM
serviceController class gives the info...

ServiceController[] services = ServiceController.GetServices();

Console.WriteLine("List of running services : ");
foreach (ServiceController service in services)
{
//If you want to check the running services only or else skip this
if (service.Status == ServiceControllerStatus.Running)
{

        if (service.ServiceName == "Name of the Service")
       {

            Console.WriteLine("Found...");
       }
}
}

This one lists only services but not device drivers, if wanted then then
ServiceController.GetDevices() method is to be used...

checking service exist or not - Venkat K replied to Sachin Mishra on 23-Apr-09 12:03 PM

Dim colServices As Object

    Dim objService As Object

    Set colServices = GetObject("winmgmts:").ExecQuery _

        ("Select Name from Win32_Service where Name = '" & Name & "'")

    For Each objService In colServices

        If Len(objService.Name) Then

            CheckService = True

        End If

    Next

    Set colServices = Nothing

End Function

 

  1. Private Sub Command2_Click()
  2.     If CheckService("Themes") Then
  3.         Debug.Print "Service Installed"
  4.     Else
  5.         Debug.Print "Service Not Installed"
  6.     End If
check if specific windows service exists in machine - mv ark replied to Sachin Mishra on 23-Apr-09 12:14 PM
The System.ServiceProcess.ServiceController class is used to detect a service and start it if required. Check this article and adapt the codeĀ  to detect the status of a service or start it if required - http://support.microsoft.com/kb/912426/en-us