C# 如何列出在 Windows 中运行的所有进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/648410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I list all processes running in Windows?
提问by Statement
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.
我想找到一种方法来遍历所有活动进程并对它们进行诊断检查(内存使用情况、CPU 时间等),有点类似于任务管理器。
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.
我什至不确定要在哪个命名空间中查看它。任何帮助/提示/链接不胜感激。
采纳答案by JaredPar
Finding all of the processes
查找所有进程
You can do this through the Process class
您可以通过 Process 类执行此操作
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
Process 类提供了一些信息,但可能会对您有所帮助。可以查询这个类
- 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)
- 所有线程
- 主窗口把手
- 所有加载的模块
- 有关内存的各种诊断信息(分页、虚拟、工作集等...)
- 基本进程信息(id、名称、磁盘位置)
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.
OP 提到他们想要获取内存和 CPU 信息。这些属性在 Process 类上很容易获得(由 GetProcesses() 返回)。下面是列出了所有支持的属性的 MSDN 页面。有各种可用的内存和 CPU 可以满足您的需求。
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
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.GetProcesses() 方法获取进程列表,如下例所示:
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
回答by CapBBeard
What operating system are you using? I take it from your C# tag that it's windows?
您使用什么操作系统?我从你的 C# 标签中得知它是 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
如果是这样,请查看 WMI,尤其是 Win32_Process 类。这是 MSDN 参考: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
以及这里的几个使用场景(例如获取进程列表):http: //msdn.microsoft.com/en-us/library/aa394599(VS.85) .aspx
回答by Mez
Finding all processes is rather easy actually:
查找所有进程实际上相当容易:
using System.Diagnostics;
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
// Get whatever attribute for process.
}
回答by Brian Rasmussen
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 call Refresh()
on the instance.
JaredPar 已经指出了Process
该类,所以我将补充一点,您应该知道,在创建实例时,该类会获取进程信息的快照。这不是实时视图。要更新它,您必须调用Refresh()
实例。
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.
最后,如果你打电话,Process.GetProcesses()
你也会得到伪进程“空闲”和“系统”。IIRC 它们具有特定的进程 ID,因此您可以轻松地将它们过滤掉。
回答by Binoj Antony
Well you can do this in powershell
那么你可以在powershell中做到这一点
1.Finding all the processes
1.查找所有进程
get-Process
2.Finding diagnostics attributes about them
2.查找有关它们的诊断属性
get-Process | where-object { $_.Handles -gt 200 }
The above code will return all processes with over 200 handles, you can specify your diagnostic attributes in this manner easily.
上面的代码将返回具有超过 200 个句柄的所有进程,您可以通过这种方式轻松指定您的诊断属性。
For more information on how to handle processes using powershell see this
有关如何使用 powershell 处理进程的更多信息,请参阅此
回答by Mahesh K Naik
using System.Diagnostics;
class Program
{
static void Main()
{
Process[] processlist = Process.GetProcesses();`
}
}
here process arrray contains all the number of process present into it.
这里的进程数组包含所有存在于其中的进程数。
回答by RollRoll
This is how I prefer to access the processes:
这是我更喜欢访问进程的方式:
static void Main(string[] args)
{
Process.GetProcesses().ToList().ForEach(p =>
{
Console.WriteLine(
p.ProcessName + " p.Threads.Count=" + p.Threads.Count + " Id=" + p.Id);
});
Console.ReadKey();
}