In this blog I am trying to make a small demo which is show all running services in C#.In this article I use the ServiceController property and get all services use ServiceController.GetServices() method below I am creating a demo project which is shows all running as well as stopped services in a datagridview:-

Steps for creating demo project:-

1.   Create a new project New >> Project (give the appropriate name of project).
2.   Now by default show a form1, in form1 drag and drop a datagridview where shows all services.
3.  Now double click on the form1 and get the form load event.
4.  In the form load event write the below code.

  private void Form1_Load(object sender, EventArgs e)
foreach (ServiceController service in ServiceController.GetServices())
string serviceName = service.ServiceName;
string serviceType = service.ServiceType.ToString();
string status = service.Status.ToString();
dgvServicesList.Rows.Add(serviceName, serviceType, status);

Code description:-
1.       This is the form load event method.

3.       This is foreach loop, this loop will run while

ServiceController.GetServices() method return value.

5.       This line of code gets the running and stopped service name and assign on string serviceName variable.

6.       This line of code gets the running and stopped service type and assign on string serviceType variable.

7.       This line of code gets the running and stopped status.

8.       This line of code set the value in datagridview.

Output Screenshot:-

  Modified On Sep-18-2014 01:24:13 PM

Leave Comment