Link to home
Start Free TrialLog in
Avatar of smaguire
smaguireFlag for Canada

asked on

How to Read CPU Temperature from C#?

hi,
Does anyone know how to read a CPU Temperature from a custom application? I can see the CPU Temperature and Fan Speed from the bios when i boot my computer, I know the computer is using a special chip to display that info "Winbond W83627THG". I know it can be done, I downloaded this software "Speed Fan" from this link:
http://www.almico.com/forumindex.php which works great but i want the CODE on how to display those info into my application

thanks

 
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Its going to be one of two things ... either they gave you an unmanaged DLL you have to call somewhere (which would kind of suck) or its just sitting in WMI which is more likely ... Have you gone to WMI and looked around to see if there is a counter already existing for it? As suggested http://blogs.msdn.com/oldnewthing/archive/2004/10/21/245652.aspx give SELECT * from Win32_BIOS and a few others whirl and I think you will be happy. SELECT * FROM Win32_TemperatureProbe looks promising as well ;-)

If its in WMI its a pretty easy task to read the value.
Avatar of smaguire

ASKER

Thanks gregoryyoung for your reply,
I am not really experienced with WMI. Do I have to create a WMI application and try to query?
I will try to re-read the article again, everything seems to be done in scripts and C nothing in C#
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wackym
wackym

Try this:
- add reference to System.Management.dll
- add using System.Management;



            ManagementClass processClass = new ManagementClass(@"root\cimv2:Win32_TemperatureProbe");
            foreach(ManagementObject service in processClass.GetInstances()) {
                Console.WriteLine("Service = " + service.GetPropertyValue("CurrentReading.Description"));
            }

Open in new window

wackym: I added your code to a button click command but it doesn't return anything:
it doesn't go into the foreach loop, it exists rightaway

any idea?
thanks,
private void button1_Click(object sender, EventArgs e)
        {
            ManagementClass processClass = new ManagementClass(@"root\cimv2:Win32_TemperatureProbe");
            foreach (ManagementObject service in processClass.GetInstances())
            {
                Console.WriteLine("Service = " + service.GetPropertyValue("CurrentReading.Description"));
            }
 
        }

Open in new window

its not as simple as that usually as the hardware vendors have not exactly standardized this ... some do it in different ways (actually some commentary on this in the link I sent see comment from xtmouse) as such you will probably have to poke around a bit ... its probably there but its probably manufacturer specific.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
wackym, I downloaded the software and its showing me alot of objects but can't for some reason call one of them, for example, i modified the code you posted like the following so i can see all instances of Win32_ComputerSystem, but its giving me an error Bot Found!

private void button1_Click(object sender, EventArgs e)
        {
            ManagementClass processClass = new ManagementClass(@"root\cimv2:Win32_ComputerSystem");
            foreach (ManagementObject service in processClass.GetInstances())
            {
                Console.WriteLine("Service = " + service.GetPropertyValue("CurrentReading.Description"));
            }

        }
now it goes into the loop but it throws an error not found. any idea why?
thanks again
Hmm, you're requesting to read a non-existing property CurrentReading.Description from Win32_ComputerSystem...you'll have to find the right property with info about CPU temperature.
Thanks wackym, I got some variables to show their values just fine. Just one question though:
Does looking up WMI parameters/variables slow down your computer? because my computer has slowed down big time, and i am thinking if i should have a timer to check the status of my CPU Temperature or not if its this slow?

thanks
It shouldn't be significantly slower ... I have code that reads <> 50 WMI values a second without any real performance cost ... I wouldn't imagine that you would want to check temperature more than every few seconds though unless you are planning to flash freeze etc the machine in testing ... its extremely rare that you change more than a 1/5 of a degree within even say a 15 or 30 second period. Even with that though I would imagine that even once every 5 seconds would give you reasonable results ...

Thank you guys