George George

George George

  • NA
  • 778
  • 0

Windows service status checking

Jun 1 2008 4:10 AM

Hello everyone,


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)?

[Code]
            ServiceController controller = new ServiceController("not existing service");
            try
            {
                if (controller.Status == ServiceControllerStatus.Running)
                {
                    // do something
                }
            }
            catch (InvalidOperationException ex)
            {
                // come here when service name does not exist
            }
        }
[/Code]


thanks in advance,
George


Answers (2)