Csharp/C#教程:如何在c#中获取CPU频率分享


如何在c#中获取CPU频率

如何获得c#的CPU频率(例如:2Ghz)? 这很简单,但我没有在环境变量中找到它。 谢谢 :)

var searcher = new ManagementObjectSearcher( "select MaxClockSpeed from Win32_Processor"); foreach (var item in searcher.Get()) { var clockSpeed = (uint)item["MaxClockSpeed"]; } 

如果你希望得到其他字段,请查看类Win32_processor

试试这个代码

 using System.Management; uint currentsp , Maxsp; public void CPUSpeed() { using(ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'")) { currentsp = (uint)(Mo["CurrentClockSpeed"]); Maxsp = (uint)(Mo["MaxClockSpeed"]); } } 

可以从注册表中获取信息,但是如果它在Windows XP或更早版本上运行(我的是Windows 7)则不知道。

 HKEY_LOCAL_MACHINE/HARDWARE/DESCRIPTION/CentralProcessor/0/ProcessorName 

读起来像

 Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz 

为了我。

像这样的代码可以检索信息(未测试):

 RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"HardwareDescriptionSystemCentralProcessor", RegistryKeyPermissionCheck.ReadSubTree); if (processor_name != null) { if (processor_name.GetValue("ProcessorNameString") != null) { string value = processor_name.GetValue("ProcessorNameString"); string freq = value.Split('@')[1]; ... } } 

(来源: 这里 )

你可以通过WMI获得它,但它很慢,所以如果你不止一次得到它我会建议你缓存它 – 类似于:

上述就是C#学习教程:如何在c#中获取CPU频率分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 namespace Helpers { using System.Management; public static class HardwareHelpers { private static uint? maxCpuSpeed = null; public static uint MaxCpuSpeed { get { return maxCpuSpeed.HasValue ? maxCpuSpeed.Value : (maxCpuSpeed = GetMaxCpuSpeed()).Value; } } private static uint GetMaxCpuSpeed() { using (var managementObject = new ManagementObject("Win32_Processor.DeviceID='CPU0'")) { var sp = (uint)(managementObject["MaxClockSpeed"]); return sp; } } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989929.html

(0)
上一篇 2021年12月24日
下一篇 2021年12月24日

精彩推荐