I am using the following code segment to check the status of a specific Windows service. My question is, if the service name does not exists, when we tries to get the Status property of the Windows service, there is InvalidOperationException. What is the elegant way to solve this issue (I have not found an API to check whether a service exists, and if I can check whether a service exists, I can check the Status property only for the service which exists)?
ServiceController controller = new ServiceController("not existing service");
if (controller.Status == ServiceControllerStatus.Running)
// do something
catch (InvalidOperationException ex)
// come here when service name does not exist
thanks in advance,