This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 2.0+

Obtaining a List of Running Processes

It can be useful to list all of the executing processes on the local machine or on a remote computer. This is possible with the .NET framework version 2.0 and the static methods of the Process class.

Process Class

The Process class can be used to obtain details of the executing processes on a computer running a Windows operating system. Several static methods are available to return arrays of Process instances, each holding information about a process and each able to manipulate the process it represents. In this article we'll see how to call these methods to generate process lists for the local machine or for a remote computer.

The Process class is found in the System.Diagnostics namespace. If you wish to recreate the sample code, start a new console application project and ensure that you include the following using directive in the automatically generated class:

using System.Diagnostics;

Listing All Processes on the Local Computer

Obtaining an array of processes on the local machine is simple. All you need to do is call the version of the GetProcesses method with no parameters. The following code uses this method to output a list to the console. Each item in the results includes a unique integer, held in the Id property and a friendly name, found in ProcessName. The MainWindowTitle property returns a string containing the name of the primary window of the process. In many cases this is empty, as background processes have no windows.

Process[] processes = Process.GetProcesses();

foreach (Process p in processes)
    Console.WriteLine("{0}:{1},{2}", p.Id, p.ProcessName, p.MainWindowTitle);

Listing All Processes on a Remote Computer

Listing the processes that are running on a remote computer is almost as simple as for the local machine. This time you use an overloaded version of GetProcesses that requires a single string argument. The string holds the name of the computer that you wish to examine. For the method to execute successfully the target machine must be contactable and the executing code must have the appropriate permissions for the remote computer.

Process[] processes = Process.GetProcesses("RemoteMachine");

foreach (Process p in processes)
    Console.WriteLine("{0}:{1},{2}", p.Id, p.ProcessName, p.MainWindowTitle);

Filtering the Processes by Name

If you are interested only in the processes that relate to a single piece of software you can use another static method. GetProcessesByName returns an array of Process objects that includes only items that have a specific friendly name.

The following line of code generates an array containing one Process for each executing instance of Notepad on the local machine. If you wanted to find a specific text file that was being edited, you could run this code and loop through the MainWindowTitle properties of the results until you identify the correct instance.

Process[] processes = Process.GetProcessesByName("notepad");

There's also a version of GetProcessesByName that can be used to interrogate a remote machine. This requires that the machine name is provided to the second parameter, as follows:

Process[] processes = Process.GetProcessesByName("notepad", "RemoteMachine");
12 May 2013