Introduction
This simple example shows how you can control a Windows Service from C# code. In this example, we are going to do the following tasks on the spooler (print) service:
- Check if it is disabled
-
Check if it is enabled
- Stop the service
- Start the service
The .NET Framework is already equipped with a class called
ServiceController
to control Windows services. With this class, you can start, stop, pause, restart and get the status from a Windows Service.
Since it is not possible to check if a Windows Service is enabled or disabled with the
ServiceController
class and to wrap everything up, I created a class with all the code inside. This class is called
Windo
wsServiceMonitor
. The class is extended with a few functions like:
Disable
(to disable the Windows Service)Enable
(to enable the Windows ServiceIsDisabled
(to check if the Windows Service is disabled)
To get these 3 functions to work, we use the
ManagementObjectSearcher
and the
Registry
class.
Using the Code
To test the
WindowsServiceMonitor
class, we create a simple Winform application as shown below:
From this form, we use the
WindowsServiceMonitor
class. Before we can use this class, we need to create it. In the form application, this is done in the Form load event.
private void MainForm_Load(object sender, EventArgs e)
{
_windowsServiceMonitor = new WindowsServiceMonitor(ServiceTextBox.Text);
}
To start the spooler, we call the
Start()
function:
_windowsServiceMonitor.Start();
To stop the spooler, we call the Stop()
function:
_windowsServiceMonitor.Stop();
To disable the spooler, we call the Disable()
function:
_windowsServiceMonitor.Disable();
To enable the spooler, we call the Enable()
function:
_windowsServiceMonitor.Enable();
All this is done through our WindowsServiceMonitor
class:
using System;
using System.Linq;
using System.Management;
using System.ServiceProcess;
using Microsoft.Win32;
namespace ServiceController
{
public class WindowsServiceMonitor
{
#region Fields
private readonly System.ServiceProcess.ServiceController _service;
#endregion
#region Properties
public string ServiceName { get; private set; }
public bool IsRunning
{
get { return _service.Status == ServiceControllerStatus.Running; }
}
public bool IsStopped
{
get { return _service.Status == ServiceControllerStatus.Stopped; }
}
public bool IsDisabled
{
get
{
try
{
var query = String.Format("SELECT *
FROM Win32_Service WHERE Name = '{0}'", _service.ServiceName);
var querySearch = new ManagementObjectSearcher(query);
var services = querySearch.Get();
foreach (var service in services.Cast<managementobject>())
return Convert.ToString(service.GetPropertyValue
("StartMode")) == "Disabled";
}
catch
{
return false;
}
return false;
}
}
public void Enable()
{
try
{
var key = Registry.LocalMachine.OpenSubKey
(@"SYSTEM\CurrentControlSet\Services\" + ServiceName, true);
if (key != null) key.SetValue("Start", 2);
}
catch (Exception e)
{
throw new Exception("Could not enable the service, error: " + e.Message);
}
}
public void Disable()
{
try
{
var key = Registry.LocalMachine.OpenSubKey
(@"SYSTEM\CurrentControlSet\Services\" + ServiceName, true);
if (key != null) key.SetValue("Start", 4);
}
catch(Exception e)
{
throw new Exception("Could not disable the service, error: " + e.Message);
}
}
public string DisplayName
{
get { return _service.DisplayName; }
}
#endregion
#region Constructor
public WindowsServiceMonitor(string serviceName)
{
_service = new System.ServiceProcess.ServiceController(serviceName);
ServiceName = _service.ServiceName;
}
#endregion
#region Start
public void Start()
{
if (_service.Status != ServiceControllerStatus.Running ||
_service.Status != ServiceControllerStatus.StartPending)
_service.Start();
_service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));
}
#endregion
#region Stop
public void Stop()
{
_service.Stop();
_service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 1, 0));
}
#endregion
#region Restart
public void Restart()
{
Stop();
Start();
}
#endregion
}
}
History