如何在 C# 中获取 CPU 速度和总物理内存?
我需要一种简单的方法来检查主机 PC 的 RAM 大小和 CPU 速度。我尝试了 WMI,但是我使用的代码
private long getCPU()
{
ManagementClass mObject = new ManagementClass("Win32_Processor");
mObject.Get();
return (long)mObject.Properties["MaxClockSpeed"].Value;
}
抛出空引用异常。此外,WMI 查询有点慢,我需要进行一些查询才能获得所有规格。有更好的办法吗?
I need a simple way of checking how much ram and fast the CPU of the host PC is. I tried WMI however the code I'm using
private long getCPU()
{
ManagementClass mObject = new ManagementClass("Win32_Processor");
mObject.Get();
return (long)mObject.Properties["MaxClockSpeed"].Value;
}
Throws a null reference exception. Furthermore, WMI queries are a bit slow and I need to make a few to get all the specs. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://dotnet-snippets.com/ dns/get-the-cpu-speed-in-mhz-SID575.aspx
RAM可以在这个SO问题中找到:如何获得计算机拥有的 RAM 总量?
http://dotnet-snippets.com/dns/get-the-cpu-speed-in-mhz-SID575.aspx
RAM can be found in this SO question: How do you get total amount of RAM the computer has?
您应该在
System.Diagnostics
中使用PerformanceCounter
类You should use
PerformanceCounter
class inSystem.Diagnostics
有关处理器的更多信息(包括其速度(以 Mhz 为单位))可在 HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor 下找到。
我正在运行 2 个 Win7x64 电脑,由于某种原因,WMI 查询在我第一次运行代码和正确的处理器时显示模糊的数字我第二次跑的速度怎么样?
当谈到性能计数器时,我确实在网络计数器上做了很多工作并获得了准确的结果,最终不得不找到更好的解决方案,所以我不相信它们!
Much about the processor including its speed in Mhz is available under HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor
I'm running 2 Win7x64 pcs and for some reason the WMI query shows a vague number the first time I run the code and the correct processor speed the second time I run it?
When it comes to performance counters, I did work a LOT with the network counters and got in accurate results and eventually had to find a better solution, so I dont trust them!