Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the CPU speed and total physical ram in C#?

Tags:

c#

system

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?

like image 273
edude05 Avatar asked Jun 10 '10 18:06

edude05


People also ask

How do I find RAM and processor info?

To check your basic computer specs in Windows 10, click on the Windows start button, then click on the gear icon for Settings. In the Windows Settings menu, select System. Scroll down and select About. From here, you will see specs for your processor, RAM, and other system info.

How can you find out what the speed of a CPU is?

If you're wondering how to check your clock speed, click the Start menu (or click the Windows* key) and type “System Information.” Your CPU's model name and clock speed will be listed under “Processor”.

How can I check my RAM specs?

Find Out How Much RAM You HaveOpen Settings > System > About and look for the Device Specifications section. You should see a line named "Installed RAM"—this will tell you how much you currently have.


2 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 total amount of RAM the computer has?

like image 103
KevenK Avatar answered Oct 04 '22 05:10

KevenK


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";
}
like image 29
Patrice Pezillier Avatar answered Oct 04 '22 03:10

Patrice Pezillier