
You want to look at all the processes that are currently running on the computer. With the Process.GetProcesses method in the System.Diagnostics namespace in the .NET Framework and C# language, we can enumerate the active processes. Process.GetProcesses is occasionally useful.
First, the Process.GetProcesses method is a public static method on the Process type. It receives no arguments or one argument of the target machine name (not shown). It returns a fully filled array of Process instances. You can use this array in a for-loop of a foreach-loop, as shown below.
This C# example demonstrates the Process.GetProcesses method in System.Diagnostics.
Program that uses GetProcesses method [C#]
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// Show all the processes on the local computer.
Process[] processes = Process.GetProcesses();
// Display count.
Console.WriteLine("Count: {0}", processes.Length);
// Loop over processes.
foreach (Process process in processes)
{
Console.WriteLine(process.Id);
}
}
}
Output
Count: 70
388
5312
1564
972
2152
936
3132
(... Rest of output was omitted for brevity.)
What are some real-world uses for the Process.GetProcesses method? You can develop a program that monitors memory usage of all the processes on the computer. Then, you can test the memory usage over time, gathering and recording statistics.
One experiment I performed was to gather the memory usage of web browsers with this method over a period of usage. Then, I was able to construct graphs of their memory usage over time. These two articles proved very popular across the Internet in 2008 and 2009.
Firefox 3 Memory Benchmarks and Comparison Chrome and Firefox 3.5 Memory UsageThe Process.GetProcesses methods allows you to acquire an array of all the current processes on the computer. This can be very helpful in certain diagnostic programs, and the method is simple to use and works reliably for this purpose.
Process.GetProcessesByName .NET Framework Info