c# - 如何在 Windows 64 位上检测 CPU 速度?

标签 c# .net windows 64-bit cpu-speed

我从这里找到了以下代码“ http://www.boyet.com/Articles/CodeFromInternet.html ”。
它返回 CPU 的速度(以 GHz 为单位),但仅适用于 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();
        }
    }
}


我搜索过 64 位管理类,但没有成功。
有没有其他方法可以获取64位Windows下的CPU速度?

最佳答案

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

  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);

关于c# - 如何在 Windows 64 位上检测 CPU 速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/336654/

相关文章:

python - 放在 QMainWindow 的 Central Widget 中的 QTabWidget 拉伸(stretch)太多,覆盖了菜单栏

c - 为什么某些 WinAPI 函数需要将结构的大小作为参数传递?

windows - VirtualBox 重启后重新连接来自 Vagrant 的同步文件夹

c# - 在 C# 中,通过 this.XYZ 访问当前对象的属性与仅 XYZ 相比被认为是糟糕的风格

c# - C#中静态类的反射

C# AppendText 从另一个线程不工作的 TextBox

c# - 为什么 DateTime 到 Unix 时间使用 double 而不是整数?

C# Sort 和 OrderBy 比较

c# - 如何实现调用自身的方法?

c# - 为什么 Adob​​e PDF 控件在外部调试时打不开? (错误 103)