如何在C#中获得CPU速度和总物理内存?

edu*_*e05 5 c# wmi system

我需要一种简单的方法来检查主机PC的内存和速度.我尝试了WMI然而我正在使用的代码

 private long getCPU()
 {
    ManagementClass mObject = new ManagementClass("Win32_Processor");
    mObject.Get();
    return (long)mObject.Properties["MaxClockSpeed"].Value;

 }
Run Code Online (Sandbox Code Playgroud)

抛出空引用异常.此外,WMI查询有点慢,我需要做一些来获得所有规格.有没有更好的办法?

Kev*_*enK 6

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;
}
Run Code Online (Sandbox Code Playgroud)

在这个SO问题中可以找到RAM:如何获得计算机的RAM总量?