How to show all services in C#

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.

1.  private void Form1_Load(object sender, EventArgs e)

2.  {

3.  foreach (ServiceController service in ServiceController.GetServices())

4.  {

5.  string serviceName = service.ServiceName;

6.  string serviceType = service.ServiceType.ToString();

7.  string status = service.Status.ToString();

8.  dgvServicesList.Rows.Add(serviceName, serviceType, status);

9.  }        

10. }

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:-

Leave Comment