Tag Archives: ServiceController
I decided to make a tutorial to help the ones that were interested to learn how to go over the Windows services so they can either start/stop/pause them or just display them in a control. The code I’ll be showing is from a program I created with the purpose of listing all the services info in a listview control.
The first steps you will need to take is reference “System.ServiceProcess” which will allow us to use the ServiceController class and also add the following code on the top of our Form/Class:
We start by declaring our ServiceController class and populating it with all the services that currently run in the system using .GetServices();
ServiceController services = ServiceController.GetServices();
Then we need to use a foreach loop in order to go through all the services. For the sake of this tutorial I’ll be using only one try/catch, usually you will want more to display more accurate info based on what info you fail to retrieve, in this case if an error occurs the whole service won’t be added to the list.
foreach (ServiceController service in services)
// Our code will go here
catch (Exception ex)
It’s time to start populating our ListView columns. The columns we will be using is the services name/status/location/description. To do that we need to first declare a new ListViewItem that we will be using to add the information. The service name and status info are easy to obtained using the ServiceController class as follows: