Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

c# - How can I list all processes running in Windows?

I would like to find a way to loop through all the active processes and do diagnostics checks on them (mem usage, cpu time etc) kinda similar to the task manager.

The problem is broken down into two parts:

  1. Finding all the processes
  2. Finding diagnostics attributes about them

I am not sure even in what namespace to go looking about it. Any help / tips / links is grateful.

question from:https://stackoverflow.com/questions/648410/how-can-i-list-all-processes-running-in-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Finding all of the processes

You can do this through the Process class

using System.Diagnostics;
...
var allProcesses = Process.GetProcesses();

Running Diagnostics

Can you give us some more information here? It's not clear what you want to do.

The Process class provides a bit of information though that might help you out. It is possible to query this class for

  • All threads
  • Main Window Handle
  • All loaded modules
  • Various diagnostic information about Memory (Paged, Virtual, Working Set, etc ...)
  • Basic Process Information (id, name, disk location)

EDIT

OP mentioned they want to get memory and CPU information. These properties are readily available on the Process class (returned by GetProcesses()). Below is the MSDN page that lists all of the supported properties. There are various memory and CPU ones available that will suite your needs.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Code:

Add this line to your using list:

using System.Diagnostics;

Now you can get a list of the processes with the Process.GetProcesses() method, as seen in this example:

Process[] processlist = Process.GetProcesses();

foreach (Process theprocess in processlist)
{
    Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...