How to Start, Restart, and Stop a Windows Service in C#

The .NET framework introduces many libraries and functions that help programmers control Microsoft Windows OS features and components. One of the features of C# that it can deal with is Windows Services, or Services. This article will learn briefly about Services and how to start, restart, and stop a service using C# programming Language.

What are Windows Services?

Windows Services are programs that run in the background of Windows NT Operation System to perform specific tasks. For example, Windows Audio (Short name AudioSrv) is a service responsible for computer audio, and the service Windows Search (short name WSearch) is the one that gives users the ability to search their computers.

Services in Microsoft Windows 10

Most essential services in windows, such as Theme or Network services, are set to start automatically when the computer starts. However, other services are disabled or set to be manually started. Those services are not used much often, so windows OS disables them to save memory space and increase the overall performance.

Start, Stop, and Restart a Service using C#

Before you get started, you will need to add the namespace below to ht header of your CSharp file.

Using System.Diagnostics;

Now you have added the namespace, let’s create the functions that will start, stop, and restart a service.

Start a Windows Service using C#

In this tutorial, we are going to use the “Themes” service for demonstration purposes.

The following example will start the windows services “Theme.”

public static void StartWindowsService(string serviceName)
{
    try
    {
        var processStartInfo = new ProcessStartInfo("net.exe", "start " + serviceName);
        processStartInfo.UseShellExecute = false;
        processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.WorkingDirectory = Environment.SystemDirectory;
        var start = Process.Start(processStartInfo);
        start.WaitForExit();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

private void btnStart_Click(object sender, EventArgs e)
{ 
        StartWindowsService("Themes");
}

As you can see, a button-click event btnStart_Click calls the function StartWindowsService to get the theme services started.

Note: This application should have administration privilege in order to Start, Stop, or Restart a Windows Service.

Stop a Windows Service using C#

The following example will stop the service “Themes”.

public static void StopWindowsService(string serviceName)
{
    try
    {
        var processStartInfo = new ProcessStartInfo("net.exe", "stop " + serviceName);
        processStartInfo.UseShellExecute = true;
        processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.WorkingDirectory = Environment.SystemDirectory;
        var start = Process.Start(processStartInfo);
        start.WaitForExit();

    }
    catch (Exception ex)
    {
        throw ex;
    }

}

private void btnStart_Click(object sender, EventArgs e)
{
    StopWindowsService("Themes");

}

Again, we are using a button to call the function StopWindowsService to stop the service “Theme”.

Restart a Windows Service using C#

To restart a service, stop it, then start it by calling the two methods above.

private void btnStart_Click(object sender, EventArgs e)
{
    string serviceName = "Themes";

    StopWindowsService(serviceName);
    StartWindowsService(serviceName);
}

Make sure to check the services in the control panel to see the change after each action.

Happy Coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *