【问题标题】:How to return a windows process list in C#?如何在 C# 中返回 Windows 进程列表?
【发布时间】:2010-01-27 02:44:23
【问题描述】:

使用 C#,如何获取当前机器上所有正在运行的进程的列表,包括图像名称、描述和进程 ID?我正在寻找类似于您在任务管理器“进程”选项卡下看到的内容,但没有资源信息。

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您需要使用诊断包中提供的实用程序来执行此操作。我找到了这段代码,它工作正常。

    这一行准确地检索进程。然后你可以提取每个进程的属性。

    `Process[] allProcs = Process.GetProcesses();

    完整代码如下。按需定制`

    using System;
    using System.Diagnostics;
    
    public class ListProcs
    {
    public static void Main()
    {
      int totMemory = 0;
      Console.WriteLine("Info for all processes:");
    
      Process[] allProcs = Process.GetProcesses();
      foreach(Process thisProc in allProcs)
      {
         string procName = thisProc.ProcessName;
         DateTime started = thisProc.StartTime;
         int procID = thisProc.Id;
         int memory = thisProc.VirtualMemorySize;
         int priMemory = thisProc.PrivateMemorySize;
         int physMemory = thisProc.WorkingSet;
         totMemory += physMemory;
         int priority = thisProc.BasePriority;
         TimeSpan cpuTime = thisProc.TotalProcessorTime;
    
         Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
         Console.WriteLine("    started: {0}", started.ToString());
         Console.WriteLine("    CPU time: {0}", cpuTime.ToString());
         Console.WriteLine("    virtual memory: {0}", memory);
         Console.WriteLine("    private memory: {0}", priMemory);
         Console.WriteLine("    physical memory: {0}", physMemory);
      }
    
      Console.WriteLine("\nTotal physical memory used: {0}", totMemory);
      }
      }
    

    引用原始article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 2014-05-24
      • 2019-01-06
      • 2010-11-05
      • 2023-03-15
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多