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

发布于 2024-07-14 15:22:54 字数 173 浏览 15 评论 0原文

我想找到一种方法来循环所有活动进程并对它们进行诊断检查(内存使用情况、CPU 时间等),类似于任务管理器。

问题分为两部分:

  1. 查找所有进程
  2. 查找有关它们的诊断属性

我什至不确定在哪个命名空间中查找它。 任何帮助/提示/链接都很感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

葮薆情 2024-07-21 15:22:54

查找所有进程

您可以通过 Process 类来完成此操作

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

运行诊断

您可以在此处给我们更多信息吗? 目前尚不清楚你想做什么。

Process 类提供了一些信息,但可能会对您有所帮助。 可以查询此类的

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

编辑< /strong>

OP 提到他们想要获取内存和 CPU 信息。 这些属性在 Process 类(由 GetProcesses() 返回)上很容易获得。 下面是列出所有支持的属性的 MSDN 页面。 有多种内存和 CPU 可供选择,以满足您的需求。

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

代码:

将此行添加到您的使用列表中:

using System.Diagnostics;

现在您可以使用 Process.GetProcesses() 方法获取进程列表,如所示这个例子:

Process[] processlist = Process.GetProcesses();

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

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);
}
往昔成烟 2024-07-21 15:22:54

查找所有进程实际上相当容易:

using System.Diagnostics;

Process[] processes = Process.GetProcesses();

foreach (Process process in processes)
{
    // Get whatever attribute for process.
}

Finding all processes is rather easy actually:

using System.Diagnostics;

Process[] processes = Process.GetProcesses();

foreach (Process process in processes)
{
    // Get whatever attribute for process.
}
暮色兮凉城 2024-07-21 15:22:54

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 call Refresh() 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.

腻橙味 2024-07-21 15:22:54

这是我更喜欢访问进程的方式:

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

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();
}
远昼 2024-07-21 15:22:54

那么你可以在 powershell 中执行此操作

1.查找所有进程

get-Process 

2.查找有关它们的诊断属性

get-Process | where-object { $_.Handles -gt 200 }

上面的代码将返回具有超过 200 个句柄的所有进程,您可以通过这种方式轻松指定诊断属性。

有关如何使用 powershell 处理进程的详细信息,请参阅此内容

Well you can do this in powershell

1.Finding all the processes

get-Process 

2.Finding diagnostics attributes about them

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.

For more information on how to handle processes using powershell see this

甜尕妞 2024-07-21 15:22:54
using System.Diagnostics;

class Program
{
   static void Main()
   {   
      Process[] processlist = Process.GetProcesses();`          
   }
}

这里的进程数组包含其中存在的所有进程号。

using System.Diagnostics;

class Program
{
   static void Main()
   {   
      Process[] processlist = Process.GetProcesses();`          
   }
}

here process arrray contains all the number of process present into it.

雨落星ぅ辰 2024-07-21 15:22:54

您使用什么操作系统? 我从你的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文