How to start, stop and verify if a service exists in C#

By FoxLearn 7/1/2024 9:38:18 AM   39
To interact with services in C#, you typically use the ServiceController class from the System.ServiceProcess namespace.

Here’s how you can start, stop, and verify the existence of a service using C#

To check if a service exists on the system, you can use the ServiceController.GetServices() method and check if your service name is in the list.

public bool ServiceExists(string serviceName)
{
    return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(serviceName));
}

It can be easily used as

string serviceName = "YourServiceName";
if (ServiceExists(serviceName))
{
     Console.WriteLine("Service exists.");
}
else
{
     Console.WriteLine("Service does not exist.");
}

To start a service, use the ServiceController.Start() method

public void StartService(string serviceName)
{
    using (ServiceController serviceController = new ServiceController(serviceName))
    {
        // Check the service if the current status is stopped.
        if (serviceController.Status != ServiceControllerStatus.Running)
        {           
            // Start the service, and wait until its status is "Running".
            serviceController.Start();
            serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
            Console.WriteLine("Service started successfully.");
        }
        else
        {
            Console.WriteLine("Service is already running.");
        }
    }
}

To stop a service, use the ServiceController.Stop() method

public void StopService(string serviceName)
{
    using (ServiceController serviceController = new ServiceController(serviceName))
    {
        // Check the service if the current status is running.
        if (serviceController.Status != ServiceControllerStatus.Stopped)
        {
            // Stop the service, and wait until its status is "Stopped".
            serviceController.Stop();
            serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
            Console.WriteLine("Service stopped successfully.");
        }
        else
        {
            Console.WriteLine("Service is already stopped.");
        }
    }
}

You can adjust the TimeSpan parameter in WaitForStatus to suit your application's requirements for waiting until the service reaches the desired state.

To verify if a service is active (running), you can use the ServiceController class in C#.

public static bool IsServiceRunning(string serviceName)
{
    using(ServiceController serviceController = new ServiceController(serviceName))
    {
         return serviceController.Status == ServiceControllerStatus.Running;
    }
}

You need to check the Status property of the ServiceController object. If the Status is ServiceControllerStatus.Running, it means the service is currently active.

To reboot a service you can modify your code as shown below.

public void RebootService(string serviceName)
{
    if (ServiceExists(serviceName))
    {
        if (serviceIsRunning(serviceName))
        {
            StopService(serviceName);
        }
        else
        {
            StartService(serviceName);
        }
    }
    else
    {
        Console.WriteLine("The service {0} doesn't exists", serviceName);
    }
}