从这篇文章:
要获取整个PC的CPU和内存使用情况:
using System.Diagnostics;
然后全局声明:
private PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");然后,要获取CPU时间,只需调用
NextValue()方法:
this.theCPUCounter.NextValue();
这将使您获得CPU使用率
至于内存使用,我相信同样的事情:
private PerformanceCounter theMemCounter = new PerformanceCounter("Memory", "Available MBytes");然后,要获取内存使用情况,只需调用
NextValue()方法:
this.theMemCounter.NextValue();
对于特定的进程CPU和内存使用情况:
private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);Process.GetCurrentProcess().ProcessName您希望获取有关信息的进程名称在哪里?
private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);Process.GetCurrentProcess().ProcessName您希望获取有关信息的进程名称在哪里?
请注意, 工作集本身可能不足以确定进程的内存占用量
-请参阅什么是专用字节,虚拟字节,工作集?
要检索所有类别,请参阅 演练:检索类别和计数器
之间的差异
Processor% Processor Time,并
Process% ProcessorTime为
Processor从PC本身
Process是每个个体的过程。因此,处理器的处理器时间将是PC上的使用时间。进程的处理器时间将是指定的进程使用率。有关类别名称的完整说明:性能监视器计数器
使用性能计数器的替代方法
使用System.Diagnostics.Process.TotalProcessorTime和System.Diagnostics.ProcessThread.TotalProcessorTime属性来计算您的处理器使用率,如本文所述。



