locked
How to check if a particular service is running

    Question

  •  

    Greetings:

     

    I have an C# application and I need it to check to see if a service is running or not.  If not, I need to start it.

     

    What does the code look like for this?

     

    Thanks

    Tuesday, May 27, 2008 1:14 PM

Answers

  • Service controller is definately the way to go. The code would look something like this...

     

    Code Snippet to Ensure a Service is Running

    private bool EnsureRunningService(string machineName, string serviceName, string[] args)

    {

    bool retVal = false;

    if (string.IsNullOrEmpty(serviceName)) throw new ArgumentException("Param cannot be null or empty!", "serviceName");

     

    string machineInternal = string.IsNullOrEmpty(machineName) ? Environment.MachineName : machineName;

     

    ServiceController[] controllers = ServiceController.GetServices(machineInternal);

     

    List<ServiceController> svcControllers = (from controller in controllers

    //where controller.DisplayName == serviceName //This would lookup by display name

    where controller.ServiceName == serviceName

    select controller).ToList();

     

    if (svcControllers.Count == 0) throw new InvalidOperationException("The service does not exist!");

    else

    {

    ServiceController primary = svcControllers[0];

    if (primary.Status != ServiceControllerStatus.Running)

    primary.Start(args);

    retVal = true;

    }

    return retVal;

    }

     

     

    I might even provide some overloads like this...

    Code Snippet for Overloading the EnsureRunningService method

    private bool EnsureRunningService(string serviceName) { return EnsureRunningService(serviceName, new string[0]); }

    private bool EnsureRunningService(string serviceName, string[] args) { return EnsureRunningService(Environment.MachineName, serviceName, args); }

    private bool EnsureRunningService(string machineName, string serviceName) { return EnsureRunningService(machineName, serviceName, new string[0]); }

     

     

    Tuesday, May 27, 2008 1:46 PM

All replies

  • Hi,

    the ServiceController class might help you out.

     

    Andrej

    Tuesday, May 27, 2008 1:35 PM
  • Service controller is definately the way to go. The code would look something like this...

     

    Code Snippet to Ensure a Service is Running

    private bool EnsureRunningService(string machineName, string serviceName, string[] args)

    {

    bool retVal = false;

    if (string.IsNullOrEmpty(serviceName)) throw new ArgumentException("Param cannot be null or empty!", "serviceName");

     

    string machineInternal = string.IsNullOrEmpty(machineName) ? Environment.MachineName : machineName;

     

    ServiceController[] controllers = ServiceController.GetServices(machineInternal);

     

    List<ServiceController> svcControllers = (from controller in controllers

    //where controller.DisplayName == serviceName //This would lookup by display name

    where controller.ServiceName == serviceName

    select controller).ToList();

     

    if (svcControllers.Count == 0) throw new InvalidOperationException("The service does not exist!");

    else

    {

    ServiceController primary = svcControllers[0];

    if (primary.Status != ServiceControllerStatus.Running)

    primary.Start(args);

    retVal = true;

    }

    return retVal;

    }

     

     

    I might even provide some overloads like this...

    Code Snippet for Overloading the EnsureRunningService method

    private bool EnsureRunningService(string serviceName) { return EnsureRunningService(serviceName, new string[0]); }

    private bool EnsureRunningService(string serviceName, string[] args) { return EnsureRunningService(Environment.MachineName, serviceName, args); }

    private bool EnsureRunningService(string machineName, string serviceName) { return EnsureRunningService(machineName, serviceName, new string[0]); }

     

     

    Tuesday, May 27, 2008 1:46 PM
  •  

    ... thanks much!
    Tuesday, May 27, 2008 2:32 PM
  • Hi,
    Nice post thanks a ton!!!!
    prayag
    Wednesday, June 11, 2008 9:41 PM
  • Thanks so much! Really helpfull.
    tushar
    Wednesday, November 12, 2008 12:26 PM