How to get CPU speed and overall physical drum in C #?

I need a simple way to check how much RAM and fast CPU the central computer. I tried WMI, but the code that I use

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

 }

Throws a null-reference exception. Also, WMI requests are a bit slow, and I need to do a few to get all the specifications. Is there a better way?

+5
source share
3 answers

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 the total amount of RAM on your computer?

+6
source

You have to use class PerformanceCounterinSystem.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";
}
+2

Much about the processor, including its speed in Mhz, can be found in the section HKEY_LOCAL_MACHINE \ HARDWARE \ DESCRIPTION \ System \ CentralProcessor

I am starting 2 Win7x64 computers, and for some reason does the WMI request show an undefined number the first time I run the code and the correct processor speed the second time I start it?

When it comes to performance counters, I worked with LOT with network meters and got accurate results, and ultimately had to find a better solution, so I don't trust them!

+1
source

All Articles