Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Control a Windows Service from Code

0.00/5 (No votes)
28 Dec 2013 1  
How to control a Windows Service from C# .NET

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 WindowsServiceMonitor. The class is extended with a few functions like:
  • Disable (to disable the Windows Service)
  • Enable (to enable the Windows Service
  • IsDisabled (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
{
    /// <summary>
    /// This class can be used to monitor a windows service
    /// </summary>
    public class WindowsServiceMonitor
    {
        #region Fields
        /// <summary>
        /// The Windows service that is controlled through the .NET ServiceController
        /// </summary>
        private readonly System.ServiceProcess.ServiceController _service;
        #endregion

        #region Properties
        /// <summary>
        /// Name of the Windows service
        /// </summary>
        public string ServiceName { get; private set; }

        /// <summary>
        /// Tells us if the Windows service is running
        /// </summary>
        public bool IsRunning
        {
            get { return _service.Status == ServiceControllerStatus.Running; }
        }
        
        /// <summary>
        /// Tells us if the Windows service is stopped
        /// </summary>
        public bool IsStopped
        {
            get { return _service.Status == ServiceControllerStatus.Stopped; }
        }

        /// <summary>
        /// Tells us if the Windows Service is disabled
        /// </summary>
        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();

                    // Since we have set the servicename in the constructor we asume the first result is always
                    // the service we are looking for
                    foreach (var service in services.Cast<managementobject>())
                        return Convert.ToString(service.GetPropertyValue
                        ("StartMode")) == "Disabled";
                }
                catch
                {
                    return false;
                }

                return false;
            }
        }

        /// <summary>
        /// Can be called to enable the Windows Service
        /// </summary>
        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);
            }
        }

        /// <summary>
        /// Disables the Windows service
        /// </summary>
        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);
            }
        }

        /// <summary>
        /// This will give us the displayname for the 
        /// Windows Service that we have set in the constructor
        /// </summary>
        public string DisplayName
        {
            get { return _service.DisplayName; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Create this class for the given service.
        /// </summary>
        /// <param name="serviceName" />
        /// The name of the service (don't use the display name!!)
        public WindowsServiceMonitor(string serviceName)
        {
            _service = new System.ServiceProcess.ServiceController(serviceName);
            ServiceName = _service.ServiceName;
        }
        #endregion

        #region Start
        /// <summary>
        /// Start the Windows service, a timeout exception will be thrown when the service
        /// does not start in one minute.
        /// </summary>
        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
        /// <summary>
        /// Stop the Windows service, a timeout exception will be thrown when the service
        /// does not start in one minute.
        /// </summary>
        public void Stop()
        {
            _service.Stop();
            _service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 1, 0));
        }
        #endregion

        #region Restart
        /// <summary>
        /// Restart the Windows service, a timeout exception will be thrown when the service
        /// does not stop or start in one minute.
        /// </summary>
        public void Restart()
        {
            Stop();
            Start();
        }
        #endregion
    }
}

History

  • First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here