How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

List all Windows services in C#

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();

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.

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:

Since the ServiceController doesn’t provide us with a way to get the services description or the services location, we will need to use the registry for that. All the services info can be located in “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services”. Inside the services folders there are a lot of key values. In our case, we are after “ImagePath” which shows the location of the service and “Description” which shows the description. In order to add the following information to our listviewitem we write the following lines:

Finally we can add the listviewitem to our listview with all the information we added to it.

Our method should look like this now:

And here’s the final result.

Keep in mind that it’s possible to go over the services by checking for the values in registry. But using this way allows easier management of the service since you can use service.Start() service.Stop() and so on while looping through them.

Hope you find this informative/useful.

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

One Response to List all Windows services in C#

  1. Muhammad moine says:

    Thanks alot sir , You help me much, by this site I got much support.
    One problem I have.
    I am quite new in window application c#, I took button from tool bar and I want to perform key press event, but here I am not able to perform this operation, when I make button enable false then key press event work properly, How to solve this problem to perform key press event and button click both in a form page,Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *