如何在Windows 64bit上检测CPU速度?

Mar*_*arc 2 .net c# windows 64-bit cpu-speed

我从这里找到了以下代码"http://www.boyet.com/Articles/CodeFromInternet.html ".
它以GHz为单位返回CPU的速度,但仅适用于32位Windows.

using System;
using System.Management;

namespace CpuSpeed
{
    class Program
    {
        static double? GetCpuSpeedInGHz()
        {
            double? GHz = null;
            using (ManagementClass mc = new ManagementClass("Win32_Processor"))
            {
                foreach (ManagementObject mo in mc.GetInstances())
                {
                    GHz = 0.001 * (UInt32) mo.Properties["CurrentClockSpeed"].Value;
                    break;
                }
            }
            return GHz;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("The current CPU speed is {0}", (GetCpuSpeedInGHz() ?? -1.0).ToString());
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


我搜索了64位管理类,但没有成功.
有没有其他方法可以在64位Windows下获得CPU速度?

Bin*_*ony 6

下面的代码应该可以解决问题

  RegistryKey registrykeyHKLM = Registry.LocalMachine;
  string keyPath = @"HARDWARE\DESCRIPTION\System\CentralProcessor\0";
  RegistryKey registrykeyCPU = registrykeyHKLM.OpenSubKey(keyPath, false);
  string MHz = registrykeyCPU.GetValue("~MHz").ToString();
  string ProcessorNameString = (string)registrykeyCPU.GetValue("ProcessorNameString");
  registrykeyCPU.Close();
  registrykeyHKLM.Close();
  Console.WriteLine("{0} MHz for {1}", MHz, ProcessorNameString);
Run Code Online (Sandbox Code Playgroud)