Вопрос

Как я могу использовать ServiceController, чтобы сказать мне, была ли зарегистрирована услуга или нет? В приведенном ниже фрагменте кода проверка на Null DisplayName приводит к System.InvalidoPerationException.

Есть ли простой способ сделать это, который мне совершенно не хватает?

ServiceController sc = new ServiceController("TestService");

if (sc.DisplayName == null)
{
     // unregistered or missing service
}
Это было полезно?

Решение

Look at solution below... It doesn't appear that you can...

From the docs:

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

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

I guess you could simply catch the exception (indicating the service doesn't exist), but that doesn't seem to be truly exceptional does it?

Solution:

Use

var services = ServiceController.GetServices(machineName)

and search the array for your needed services. If you're just working on your local box you can omit the machineName argument.

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top