Checking Existing Windows Service

  • Thread starter Thread starter Ann Marinas
  • Start date Start date
A

Ann Marinas

Hi, All!

I just would like to ask... Is it possible that you can detect whether a
Windows Service is existing or not? If this is possible, how can you
implement it?

Thank you!

A
 
Ann,

The easiest way would be to use the classes in the System.Management
namespace to perform a WMI query for the instance of Win32_Service that
corresponds to the service you are looking for. This will let you determine
whether the service exists, is it running, etc, etc.

Hope this helps.
 
Thank you so much. I really do appreciate the help!

I'll try to apply this on my application.

Thank you!

Ann

Nicholas Paldino said:
Ann,

The easiest way would be to use the classes in the System.Management
namespace to perform a WMI query for the instance of Win32_Service that
corresponds to the service you are looking for. This will let you determine
whether the service exists, is it running, etc, etc.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ann Marinas said:
Hi, All!

I just would like to ask... Is it possible that you can detect whether a
Windows Service is existing or not? If this is possible, how can you
implement it?

Thank you!

A
 
If you don't want to work through the WMI query syntax, this may be a little
easier:
//required for using ServiceController
using System.ServiceProcess;

private bool ServiceExists(string serviceName)
{
ServiceController[] allServices = ServiceController.GetServices();
bool result = false;
ServiceController sc = null;
for (int i = 0; i < allServices.Length; i++)
{
if (allServices.ServiceName.ToLower() == serviceName.ToLower())
{
result = true;
break;
}
}
return result;
}
 
Back
Top