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

By FoxLearn 1/22/2025 4:42:28 AM   532
To interact with services in C#, you typically use the ServiceController class from the System.ServiceProcess namespace.

For example, how you can start, stop, and verify the existence of a service in 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.

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

To check if a service exists, you can write as shown below.

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

// c# start service
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

// c# stop service
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.

C# check if service is running

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

// c# check if service is running
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.

// c# restart service
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);
    }
}