如何列出在Windows中运行的所有进程?出在、进程、Windows

2023-09-02 01:37:36 作者:纯爷们纵爱成欢、。

我想找到一种方法来遍历所有的活动进程,并做诊断对他们的检查(内存使用,CPU时间等),有点类似于任务管理器。

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:

查找所有进程 查找诊断属性对他们

我不知道,即使在去寻找它的命名空间,。任何帮助/技巧/链接感谢。

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

推荐答案

查找所有进程

您可以通过过程来实现这个目的

You can do this through the Process class

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

运行诊断程序

你能不能给我们一些更多的信息吗?目前尚不清楚你想要做什么。

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

所有线程 在主窗口的句柄 所有加载的模块 有关内存的各种诊断信息(分页,虚拟,工作集等) 基本过程信息(ID,姓名,磁盘位置)

修改

欧普提到他们想要得到的内存和CPU的信息。这些属性都是现成的工艺类(由GetProcesses()返回)。下面是MSDN页面,列出了所有支持的属性。有可用,将适合您的需求的各种内存和CPU的。

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;

现在,你可以得到的进程列表与Process.GetProcesses()方法,如在这个例子中看到的:

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);
}