Thread: Windows service status checking

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Windows service status checking

    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
                }
            }

    thanks in advance,
    George

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    If you want to check if a service exists, the static method GetServices will return an array of all existing services.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. windows service + hooks + dll
    By rotis23 in forum Windows Programming
    Replies: 5
    Last Post: 09-04-2003, 07:06 AM
  4. Disable Messenger Service Windows
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 08-22-2003, 05:07 PM
  5. Windows Error checking
    By WebSnozz in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2002, 10:00 PM