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 »

Visual Studio 2013

Visual Studio 2013

Visual Studio 2013 is now available. Make sure to upgrade to the latest version of Visual Studio. 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 Windows active processes in C#

Since some people were interested on how to get all the Window processes, I decided to make this tutorial. How you tailor it to your needs is up to you.

We start off by referencing System.Management and adding the following on the top of our Form/Class:

Then we need to declare our main classes. In this case that would be ManagementClass and ManagementObjectCollection in order to help us loop through the active process. Note that this can also be done by Process.GetProcesses() but there is a limitation. Process.GetProcesses() can only see the same bit process as your .exe files. So using that mean we wont be able to see all the processes, hence why we are using the two classes I mentioned above instead.

After that we need to create our foreach loop which will help us get the information for each process we find and also a new listviewitem which we will be adding to our listview at the end of the loop so we can see the results.

For the sake of this tutorial we will only be concentrating in the process id/name/location and finally the description.

First in order to get the process ID we need to use our ManagementObject class to get the Process ID object. Then since our listview requires string we need to convert it into a string. This is done using the following line:

Next in the list is the process name. Just like above we need to use the ManagementObject to get the object for the process name which then needs to be converted into a string in order to use it for our listview:

The same goes for the process location:

Now for the tricky part. If you use the Win32_Process class to get the description the results will be inaccurate since it returns the name of the process instead of the actual program description. In order to counter that we will need to use the FileVersionInfo class to get the description of the process from the path we are going to specify. Since not all process have a description available we will need to use a try/catch block for our next lines to avoid getting a program exception when it returns null:

Then finally after we all add the info to our listview item we are free to add the item to our listview itself

Our method should look like this now:

And this is our final result:

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 Windows active processes in C#

  1. KAMAL says:

    SIR I NEED THIS WHOLE CODE . BCOZ ITS DIDNT WORK FOR ALL PROCESSES

Leave a Reply

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