Control Windows Services in windows application
In this article i am trying to explain what Windows Services are and How to control those Services in windows application like start, stop, disable, getting list of all Services and their status and also discussed regarding namespace and class used to achieve these functions in windows application.
I have added very simple code for explaining these things.
I will start with, What is Windows Service?
Windows Service is an application that run in a separate windows session to perform specific function. Most of services starts automatically when computer boots and run in background but also you can configure to start, stop, pause and disable services manually(Administrative Tools->Services). Services doesn't have UI to interact directly.
Service related all Classes, Structures and Enumerations present under namespace System.ServiceProcess in C#. "ServiceProcess" provides class "ServiceController" which derived from "Component" class. ServiceController class contains Methods, Properties and Events that helps to control Services.
Namespace: System.ServiceProcess
Assembly: System.ServiceProcess.dll
Let's consider a service named "MyService" present in local machine. First you create a ServiceController object and then call its StartMyService or StopMyService methods to start and stop a windows service.
//Namespace
using System;
using System.ServiceProcess ;
//To Stop running "MyService"
public void StopMyService()
{
ServiceController mySc = new ServiceController("MyService");
if ((mySc.Status.Equals(ServiceControllerStatus.Running)) || (mySc.Status.Equals(ServiceControllerStatus.StartPending)))
mySc.Stop();
}
//To Start stopped "MyService"
public void StartMyService()
{
ServiceController mySc = new ServiceController("MyService");
if ((mySc.Status.Equals(ServiceControllerStatus.Stopped)) || (mySc.Status.Equals(ServiceControllerStatus.StopPending)))
mySc.Start();
}
ServiceController class also provide Pause, Continue, and Refresh methods to pause, continue, and refresh a Windows Service.
ServiceController class doesn't support to disable/enable Services but we can disable Services by two other ways...
//By setting Resistry key value
public void DisableMyService()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\MyService", true);
key.SetValue("Start", 4);
}
Set THE type...
2 - Automatic
3 - Manual
4 - Disabled
//By using executable sc.exe
public void DisableMyService()
{
Process sc = Process.Start("sc.exe", "config [MyService] start= disabled");
}
//Returns the list of all non device driver Services from local machine
public List
{
List
ServiceController[] srvcs = ServiceController.GetServices();
foreach (ServiceController service in srvcs)
{
string serviceName = service.ServiceName;
string serviceDisplayName = service.DisplayName;
string serviceType = service.ServiceType.ToString();
string status = service.Status.ToString();
lstServices.Items.Add(serviceName + " " + serviceDisplayName +
serviceType + " " + status);
}
return lstServices;
}
Also you can get status of "MyService" by using method like...
public string GetMyServiceStatus()
{
ServiceController srvc = new ServiceController("MyService");
switch (srvc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Pending";
}
}
I hope you have got some minimum idea to use "ServiceControler" class and also how to control "Services" by using ServiceControler class methods, properties and events.