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:
1 2 | using System.Management; using System.Diagnostics; |
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.
1 2 | ManagementClass management = new ManagementClass("Win32_Process"); ManagementObjectCollection mCollection = management.GetInstances(); |
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.
1 2 3 4 | foreach (ManagementObject process in mCollection) { ListViewItem newListViewItem = new ListViewItem(); } |
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:
1 | newListViewItem.Text = (process["ProcessId"].ToString()); |
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:
1 | newListViewItem.SubItems.Add((string)process["Name"]); |
The same goes for the process location:
1 | newListViewItem.SubItems.Add((string)process["ExecutablePath"]); |
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:
1 2 3 4 5 6 7 8 9 | try { FileVersionInfo info = FileVersionInfo.GetVersionInfo((string)process["ExecutablePath"]); newListViewItem.SubItems.Add(info.FileDescription); } catch { newListViewItem.SubItems.Add("N/A"); } |
Then finally after we all add the info to our listview item we are free to add the item to our listview itself
1 | listView1.Items.Add(newListViewItem); |
Our method should look like this now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | ManagementClass management = new ManagementClass("Win32_Process"); ManagementObjectCollection mCollection = management.GetInstances(); foreach (ManagementObject process in mCollection) { ListViewItem newListViewItem = new ListViewItem(); newListViewItem.Text = (process["ProcessId"].ToString()); newListViewItem.SubItems.Add((string)process["Name"]); newListViewItem.SubItems.Add((string)process["ExecutablePath"]); try { FileVersionInfo info = FileVersionInfo.GetVersionInfo((string)process["ExecutablePath"]); newListViewItem.SubItems.Add(info.FileDescription); } catch { newListViewItem.SubItems.Add("N/A"); } istView1.Items.Add(newListViewItem); } |
And this is our final result:
Thanks. Just what I needed.
SIR I NEED THIS WHOLE CODE . BCOZ ITS DIDNT WORK FOR ALL PROCESSES