[英]How do I get the CPU speed and total physical ram in C#?
I need a simple way of checking how much ram and fast the CPU of the host PC is. 我需要一种简单的方法来检查主机PC的内存和速度。 I tried WMI however the code I'm using
我尝试了WMI然而我正在使用的代码
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.
此外,WMI查询有点慢,我需要做一些来获得所有规格。 Is there a better way?
有没有更好的办法?
http://dotnet-snippets.com/dns/get-the-cpu-speed-in-mhz-SID575.aspx http://dotnet-snippets.com/dns/get-the-cpu-speed-in-mhz-SID575.aspx
using System.Management;
public uint CPUSpeed()
{
ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
uint sp = (uint)(Mo["CurrentClockSpeed"]);
Mo.Dispose();
return sp;
}
RAM can be found in this SO question: How do you get total amount of RAM the computer has? 在这个SO问题中可以找到RAM: 如何获得计算机的RAM总量?
You should use PerformanceCounter
class in System.Diagnostics
您应该在
System.Diagnostics
使用PerformanceCounter
类
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
public string getCurrentCpuUsage(){
cpuCounter.NextValue()+"%";
}
public string getAvailableRAM(){
ramCounter.NextValue()+"MB";
}
Much about the processor including its speed in Mhz is available under HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor 很多关于处理器,包括它的Mhz速度,可在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? 我正在运行2个Win7x64个电脑,由于某种原因,WMI查询显示第一次运行代码时的模糊数字以及第二次运行时的正确处理器速度?
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! 当涉及到性能计数器时,我确实与网络计数器一起工作并得到了准确的结果,最终不得不找到更好的解决方案,所以我不相信它们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.