A service is an application almost like any other, the only difference between services and other programs is that they run in the background and don't have an user interface you can click or tap on. In this article, you will learn how to verify if a service exists in Windows, how to start it or stop it according to your needs in a WinForms application easily.
1. Add a reference to ServiceProcess.dll
In order to stop or start a service, we will need the ServiceController class. This class represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it. This function is included in the System.ServiceProcess.dll
assembly that can't be imported directly with your code into your class.
As first step, do right click on your project in the Solution Explorer, then select Add and from the Drop-Down menu select Reference.
Now, from the emergent window go to the Framework tab on the left side and search for System.ServiceProcess option in the list. Select it and click on ok.
The DLL should be now imported in your project and ready to use.
2. Import the class
Import the following type with the using directive in the top of your class:
using System.ServiceProcess;
And now you will be able to use the ServiceController class that we'll need to start or stop a service.
3. Functions
Now, use the ServiceController class to achieve all of the possible tasks with a windows service as shown in the following examples
Note
Remember to add using System.ServiceProcess;
at the top of your class in order to access the services classes. Besides, all these methods will work with the name of the service, you can see the list of all the installed services in windows opening services.msc
A. Verify if a service exists
To verify if a service exists, you can create the following custom bool method that verifies if a service exists by it's name:
/// <summary>
/// Verify if a service exists
/// </summary>
/// <param name="ServiceName">Service name</param>
/// <returns></returns>
public bool serviceExists(string ServiceName)
{
return ServiceController.GetServices().Any(serviceController => serviceController.ServiceName.Equals(ServiceName));
}
And it can be easily used as:
if (serviceExists("mysql"))
{
Console.WriteLine("Service exists");
}
else
{
Console.WriteLine("Service doesn't exists");
}
B. Start a service
Use the following method to start a service by its name:
/// <summary>
/// Start a service by it's name
/// </summary>
/// <param name="ServiceName"></param>
public void startService(string ServiceName)
{
ServiceController sc = new ServiceController();
sc.ServiceName = ServiceName;
Console.WriteLine("The {0} service status is currently set to {1}", ServiceName, sc.Status.ToString());
if (sc.Status == ServiceControllerStatus.Stopped)
{
// Start the service if the current status is stopped.
Console.WriteLine("Starting the {0} service ...", ServiceName);
try
{
// Start the service, and wait until its status is "Running".
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
// Display the current service status.
Console.WriteLine("The {0} service status is now set to {1}.", ServiceName , sc.Status.ToString());
}
catch (InvalidOperationException e)
{
Console.WriteLine("Could not start the {0} service.", ServiceName);
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("Service {0} already running.", ServiceName);
}
}
C. Stop a service
Use the following method to stop an installed service by its name:
/// <summary>
/// Stop a service that is active
/// </summary>
/// <param name="ServiceName"></param>
public void stopService(string ServiceName)
{
ServiceController sc = new ServiceController();
sc.ServiceName = ServiceName;
Console.WriteLine("The {0} service status is currently set to {1}", ServiceName , sc.Status.ToString());
if (sc.Status == ServiceControllerStatus.Running)
{
// Start the service if the current status is stopped.
Console.WriteLine("Stopping the {0} service ...", ServiceName);
try
{
// Start the service, and wait until its status is "Running".
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
// Display the current service status.
Console.WriteLine("The {0} service status is now set to {1}.", ServiceName, sc.Status.ToString());
}
catch (InvalidOperationException e)
{
Console.WriteLine("Could not stop the {0} service.", ServiceName);
Console.WriteLine(e.Message);
}
}
else
{
Console.WriteLine("Cannot stop service {0} because it's already inactive.", ServiceName);
}
}
D. Verify if a service is active
As you can see, in the previous methods you can't start a service that's already active or stop a service that's already inactive. If you're working in a "Reboot service" function, you can create the following method to verify if a service is running:
/// <summary>
/// Verify if a service is running.
/// </summary>
/// <param name="ServiceName"></param>
public bool serviceIsRunning(string ServiceName)
{
ServiceController sc = new ServiceController();
sc.ServiceName = ServiceName;
if (sc.Status == ServiceControllerStatus.Running)
{
return true;
}
else
{
return false;
}
}
E. Reboot a service
To reboot a service, you will need all of the previous methods included in your class:
/// <summary>
/// Reboots a service
/// </summary>
/// <param name="ServiceName"></param>
public void rebootService(string ServiceName)
{
if (serviceExists(ServiceName))
{
if (serviceIsRunning(ServiceName))
{
stopService(ServiceName);
}
else
{
startService(ServiceName);
}
}else
{
Console.WriteLine("The given service {0} doesn't exists", ServiceName);
}
}
And use it like:
rebootService("mysql");
Happy coding !