如何列出 Windows 中运行的所有进程?
我想找到一种方法来循环所有活动进程并对它们进行诊断检查(内存使用情况、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:
- Finding all the processes
- Finding diagnostics attributes about them
I am not sure even in what namespace to go looking about it. Any help / tips / links is grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查找所有进程
您可以通过 Process 类来完成此操作
运行诊断
您可以在此处给我们更多信息吗? 目前尚不清楚你想做什么。
Process 类提供了一些信息,但可能会对您有所帮助。 可以查询此类的
编辑< /strong>
OP 提到他们想要获取内存和 CPU 信息。 这些属性在 Process 类(由 GetProcesses() 返回)上很容易获得。 下面是列出所有支持的属性的 MSDN 页面。 有多种内存和 CPU 可供选择,以满足您的需求。
http://msdn.microsoft.com/en-us/library /system.diagnostics.process.aspx
代码:
将此行添加到您的使用列表中:
现在您可以使用 Process.GetProcesses() 方法获取进程列表,如所示这个例子:
Finding all of the processes
You can do this through the Process class
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
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:
Now you can get a list of the processes with the Process.GetProcesses() method, as seen in this example:
查找所有进程实际上相当容易:
Finding all processes is rather easy actually:
JaredPar 已经指出了 Process 类,所以我只是补充一下,您应该知道,该类在创建实例时会拍摄进程信息的快照。 这不是实时取景。 要更新它,您必须在实例上调用
Refresh()
。另请记住,在您检查进程时该进程可能会关闭,因此请准备好捕获异常并进行相应的处理。
最后,如果您调用 Process.GetProcesses(),您还将获得伪进程“idle”和“system”。 IIRC 它们有特定的进程 ID,因此您可以轻松地将它们过滤掉。
JaredPar already pointed out the
Process
class, so I'll just add, that you should be aware, that the class takes a snapshot of the process' information when the instance is created. It is not a live view. To update it you have to callRefresh()
on the instance.Also keep in mind, that the process may close while you are inspecting it, so be prepared to catch exceptions and handle them accordingly.
And finally if you call
Process.GetProcesses()
you will also get the pseudo processes "idle" and "system". IIRC they have specific process IDs so you can easily filter them out.这是我更喜欢访问进程的方式:
This is how I prefer to access the processes:
那么你可以在 powershell 中执行此操作
1.查找所有进程
2.查找有关它们的诊断属性
上面的代码将返回具有超过 200 个句柄的所有进程,您可以通过这种方式轻松指定诊断属性。
有关如何使用 powershell 处理进程的详细信息,请参阅此内容
Well you can do this in powershell
1.Finding all the processes
2.Finding diagnostics attributes about them
The above code will return all processes with over 200 handles, you can specify your diagnostic attributes in this manner easily.
For more information on how to handle processes using powershell see this
这里的进程数组包含其中存在的所有进程号。
here process arrray contains all the number of process present into it.
您使用什么操作系统? 我从你的 C# 标签中得知它是 Windows?
如果是这样,请检查 WMI,特别是 Win32_Process 类。 以下是 MSDN 参考:http://msdn.microsoft。 com/en-us/library/aa394372(VS.85).aspx
这里还有一些使用场景(例如获取进程列表): microsoft.com/en-us/library/aa394599(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa394599(VS.85).aspx
What operating system are you using? I take it from your C# tag that it's windows?
If so, check out WMI, particularly the Win32_Process class. Here's the MSDN reference: http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx
As well as a couple of usage scenarios here (such as getting a list of processes): http://msdn.microsoft.com/en-us/library/aa394599(VS.85).aspx