Working with windows service - C#

System.ServiceProcess.ServiceController is the class provided as a part of .net framework to work with windows services.

As we can see on msdn:

"System.ServiceProcess.ServiceController Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it."

However as you can feel the methods provided by this class are not enough and moreover you need to write a whole new wrapper around it to work with it. I made a wrapper for this class so that I can work with windows services more easily.

Stop Windows Service

static void StopService(string serviceName, int timeoutMilliseconds)
{
  try
  {
    var service = new ServiceController(serviceName);
    var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    service.Stop();
    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
  }
  catch (Exception e)
  {
    MessageBoxService.Run(e.Message + e.InnerException);
  }
}

Start Windows Service

static void StartService(string serviceName, int timeoutMilliseconds)
{
  try
  {
    var service = new ServiceController(serviceName);
    var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch (Exception e)
  {
    MessageBoxService.Run(e.Message + e.InnerException);
  }
}

Delete Windows Service

static void DeleteService(string serviceName)
{
  try
  {
    var process = Process.Start("sc", string.Format("delete \"{0}\"", serviceName));
    process.WaitForExit();
  }
  catch (Exception e)
  {
    MessageBoxService.Run(e.Message + e.InnerException);
  }
}

Get Windows Services

static ServiceController[] GetServices(string nameFilter, string directoryFilter)
{
  var res = new List<ServiceController>();
  var services = ServiceController.GetServices();
  foreach (ServiceController service in services)
  {
    var add = true;

    if (!string.IsNullOrEmpty(nameFilter))
      add = service.DisplayName.ToLower().Contains(nameFilter);

    if (add && !string.IsNullOrEmpty(directoryFilter))
    {
      var dir = GetPathToTheExecutable(service.ServiceName);
      add = dir.ToLower().Contains(directoryFilter.ToLower());
    }
    if (add)
      res.Add(service);
  }
  return res.ToArray();
}

Get path to the executable of the Windows Service

static string GetPathToTheExecutable(string serviceName)
{
  var machineName = Environment.MachineName;
  var registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
  var keyHKLM = Registry.LocalMachine;

  RegistryKey key;
  if (machineName != "")
  {
    key = RegistryKey.OpenRemoteBaseKey
      (RegistryHive.LocalMachine, machineName).OpenSubKey(registryPath);
  }
  else
  {
    key = keyHKLM.OpenSubKey(registryPath);
  }
  var value = key.GetValue("ImagePath").ToString();
  key.Close();
  return value;
}



References


  • http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx


No comments:

Post a Comment