Checking unregistered / missing service

How can I use a ServiceController to tell me if a service has been registered or not? In the code snippet below, checking for null DisplayName throws a System.InvalidOperationException.

Is there an easy way to do this that I am completely missing?

ServiceController sc = new ServiceController("TestService");

if (sc.DisplayName == null)
{
     // unregistered or missing service
}

      

+2


source to share


1 answer


Take a look at the solution below ... Doesn't seem like you can ...

From the docs :

You can use the ServiceController class to connect and control the behavior of existing services.

What reads like services must already be registered in order for the ServiceController to work with them.

I think you could just catch the exception (showing that the service doesn't exist), but doesn't that seem really exceptional?



Decision:

Use

var services = ServiceController.GetServices(machineName)

      

and search for an array for your required services. If you are just working in your local field, you can omit the argument machineName

.

http://msdn.microsoft.com/en-us/library/s21fd6th.aspx

+3


source







All Articles