With the .NET framework it is quite easily possible to control Windows Services programmatically. In this article I will briefly demonstrate how this can be done. To get to most methods relating to Windows Services, you need access to the System.ServiceProcess namespace. The method call to perform operations are then relatively simple.

Installing a Windows Service

First of all, let's take a look at installing a Windows Service programmatically. I use the following function to install a service, specifying the Service's .exe file as a parameter. The code look as follows:

   1: using System.ServiceProcess;
   2:  
   3: private bool InstallService(string FilePath)
   4: {
   5:     try
   6:     {
   7:         // Install Service
   8:         ManagedInstallerClass.InstallHelper(new string[] { FilePath });
   9:  
  10:         // Set Logon User
  11:         string objPath = string.Format("Win32_Service.Name='{0}'", "ServiceName");
  12:         using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
  13:         {
  14:             object[] wmiParams = new object[11];
  15:             // User Name
  16:             wmiParams[6] = "LocalSystem";
  17:             // Password
  18:             wmiParams[7] = "";
  19:             service.InvokeMethod("Change", wmiParams);
  20:         }
  21:  
  22:         return true;
  23:     }
  24:     catch (Exception ex)
  25:     {
  26:         return false;
  27:     }
  28: }

The installation consists of only one line of code (line 8), but after that I set the logon user for the Service. In my case the service had to log on using the Local System account. If you do not need to change this, that part of the code is not necessary.

Starting and stopping Windows Service

Next, we look at starting and stopping our service. The following code performs those functions:

   1: private bool StartService(string serviceName)
   2: {
   3:     // Get list of available services
   4:     ServiceController[] services = ServiceController.GetServices();
   5:     // Loop through services
   6:     for (int i = 0; i < services.Length; i++)
   7:     {
   8:         // Check if service is the one we need
   9:         if (services[i].ServiceName == serviceName)
  10:         {
  11:             // Get a controller object for this service
  12:             ServiceController sc = new ServiceController(services[i].DisplayName, Environment.MachineName);
  13:             // If the service is not running yet, start it
  14:             if (sc.Status != ServiceControllerStatus.Running)
  15:                 sc.Start();
  16:         }
  17:     }
  18: }
  19:  
  20: private bool StopService(string serviceName)
  21: {
  22:     // Get list of available services
  23:     ServiceController[] services = ServiceController.GetServices();
  24:     // Loop through services
  25:     for (int i = 0; i < services.Length; i++)
  26:     {
  27:         // Check if service is the one we need
  28:         if (services[i].ServiceName == serviceName)
  29:         {
  30:             // Get a controller object for this service
  31:             ServiceController sc = new ServiceController(services[i].DisplayName, Environment.MachineName);
  32:             // If the service is not stopped, stop it now
  33:             if (sc.Status != ServiceControllerStatus.Stopped)
  34:                 sc.Stop();
  35:         }
  36:     }
  37: }

And those are the basics of controlling Windows Services. There are some more methods available, like pausing and resuming a service, but you can easily adapt the code above to get those functions.

If you have any problems or questions about this code, leave a comment, and I'll try to help out.