Click here to Skip to main content
Click here to Skip to main content

How to Control a Windows Service from Code

, 28 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kees van Spelde
Software Developer (Senior)
Netherlands Netherlands
Programming since I was a kid. Started on the Commodore 64 with BASIC. Since then I used programming languages like Turbo Pascal, Delphi, C++ and Visual Basic 6.
 
Now a days I do a lot of programming in C# with underlying databases like MS SQL
Follow on   LinkedIn

Comments and Discussions

 
QuestionHow to Send and receive the Dataset From Window Service PinmemberMaria Norbert21-May-14 20:28 
AnswerRe: How to Send and receive the Dataset From Window Service PinprofessionalKees van Spelde22-May-14 4:16 
GeneralMy vote of 5 Pinmemberjohannesnestler9-Jan-14 3:44 
QuestionAbout more practical situations Pinmemberwmjordan28-Dec-13 18:18 
AnswerRe: About more practical situations Pinmembermav.northwind28-Dec-13 21:39 
GeneralRe: About more practical situations PinmemberKees van Spelde29-Dec-13 1:59 
AnswerRe: About more practical situations Pinmemberjohannesnestler9-Jan-14 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 28 Dec 2013
Article Copyright 2013 by Kees van Spelde
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid