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

发布于 2024-09-04 18:21:43 字数 321 浏览 5 评论 0原文

我需要一种简单的方法来检查主机 PC 的 RAM 大小和 CPU 速度。我尝试了 WMI,但是我使用的代码

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

 }

抛出空引用异常。此外,WMI 查询有点慢,我需要进行一些查询才能获得所有规格。有更好的办法吗?

I need a simple way of checking how much ram and fast the CPU of the host PC is. I tried WMI however the code I'm using

 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. Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽梦忆笙歌 2024-09-11 18:21:43

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可以在这个SO问题中找到:如何获得计算机拥有的 RAM 总量?

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?

娇俏 2024-09-11 18:21:43

您应该在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";
}

You should use PerformanceCounter class in System.Diagnostics

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";
}
寄居者 2024-09-11 18:21:43

有关处理器的更多信息(包括其速度(以 Mhz 为单位))可在 HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor 下找到。

我正在运行 2 个 Win7x64 电脑,由于某种原因,WMI 查询在我第一次运行代码和正确的处理器时显示模糊的数字我第二次跑的速度怎么样?

当谈到性能计数器时,我确实在网络计数器上做了很多工作并获得了准确的结果,最终不得不找到更好的解决方案,所以我不相信它们!

Much about the processor including its speed in Mhz is available under 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?

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文