使用 C# 在 WMI 中返回 CPU 使用率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9777661/
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
Returning CPU usage in WMI using C#
提问by jpavlov
采纳答案by L.B
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
foreach (ManagementObject obj in searcher.Get())
{
var usage = obj["PercentProcessorTime"];
var name = obj["Name"];
Console.WriteLine(name +" : " + usage);
}
And for Linq lovers
对于 Linq 爱好者
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
var cpuTimes = searcher.Get()
.Cast<ManagementObject>()
.Select(mo => new
{
Name = mo["Name"],
Usage = mo["PercentProcessorTime"]
}
)
.ToArray();
回答by jgstew
It seems like the info is also available in WMI here:
似乎信息也可以在 WMI 中找到:
select LoadPercentage from Win32_Processor
select LoadPercentage from Win32_Processor
"Load capacity of each processor, averaged to the last second. Processor loading refers to the total computing burden for each processor at one time."
“每个处理器的负载能力,平均到最后一秒。处理器负载是指每个处理器在一次的总计算负担。”
OR:
或者:
select LoadPercentage from CIM_Processor
select LoadPercentage from CIM_Processor
"Loading of the processor, averaged over the last minute, in a percentage."
“处理器的负载,在最后一分钟的平均值,以百分比表示。”
OR:
或者:
select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processoralso seems to work.
select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor似乎也有效。
Note:these often return multiple results per CPU core and have to be summed to get the total CPU usage for the system as a whole, so look for that.
注意:这些通常会为每个 CPU 内核返回多个结果,并且必须求和才能获得整个系统的总 CPU 使用率,因此请寻找它。
This question and answer really has more to do with WMI since getting info from WMI with C# is really a different question and should be very similar for any WMI query in C#.
这个问题和答案确实与 WMI 有更多关系,因为从 WMI 使用 C# 获取信息确实是一个不同的问题,对于 C# 中的任何 WMI 查询应该非常相似。

