Avatar of smaguire
smaguire
Flag 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

 
.NET ProgrammingMicrosoft Development

Avatar of undefined
Last Comment
smaguire

8/22/2022 - Mon
gregoryyoung

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.
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
gregoryyoung

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
smaguire

ASKER
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

gregoryyoung

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
wackym

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
smaguire

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
wackym

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.
smaguire

ASKER
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
gregoryyoung

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 ...

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
smaguire

ASKER
Thank you guys