Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am trying to retrieve a computer's CPU temp. I have posted my code below. Can someone please tell me where my problem is?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace SServer.Common.Diagnostics.SystemInfo
{
    public class Temperature
    {
        public double CurrentValue { get; set; }
        public string InstanceName { get; set; }
        public static List<string> Temperatures
        {
            get
            {
                List<string> result = new List<string>();

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_TemperatureProbe");

                foreach (ManagementObject obj in searcher.Get())
                {
                    if (obj != null)
                    {
                        Double temp = Convert.ToDouble(obj["CurrentTemperature"].ToString());

                        temp = (temp - 2732) / 10.0;

                        result.Add(new Temperature
                        {
                            CurrentValue = temp,
                            InstanceName = obj["CurrentTemperature"].ToString()
                        }.ToString());
                    }
                }
                return result;
            }
        }
    }
}
Posted
Updated 4-Feb-18 18:00pm
Comments
[no name] 10-Oct-12 11:16am    
What problem?
Christopher Smit 10-Oct-12 11:24am    
Double temp = Convert.ToDouble(obj["CurrentTemperature"].ToString());

This line shows the following Execption.

ManagementException was unhandled - Not Found
Richard MacCutchan 10-Oct-12 12:00pm    
Looks like obj["CurrentTemperature"] does not exist.

1 solution

CurrentTemperature does not exist - it is CurrentReading.

But I have more bad news for you: that probably won't work either.
See MSDN: Win32_TemperatureProbe class[^]

"Most of the information that the Win32_TemperatureProbe WMI class provides comes from SMBIOS. Real-time readings for the CurrentReading property cannot be extracted from SMBIOS tables. For this reason, current implementations of WMI do not populate the CurrentReading property. The CurrentReading property's presence is reserved for future use."
 
Share this answer
 
Comments
Christopher Smit 10-Oct-12 15:11pm    
Is there really NO WAY to do this? I mean there must be something... Right?
OriginalGriff 10-Oct-12 15:25pm    
As far as I know, no, there is no generic way to get this. The problem is that WMI (et al) rely on motherboard manufacturers providing that info via their drivers - and most don't bother.
Have a google, and you will see what I mean.
Sorry to be the bearer of bad news, but...
Christopher Smit 10-Oct-12 15:27pm    
Well that sucks.. Would have been pretty epic being able to do that.. Thanks anyway :)
Christopher Smit 15-Oct-12 14:59pm    
I've done a little research.. How do programs like CPUID get the Temperature for the CPU?
Urutar 29-Jun-15 9:25am    
They have implemented functions using the documentation for the specific APIs from the manufacturers of the different components.

You can find more information about something similar here:
http://stackoverflow.com/a/9564862/1648463

It is about Open Hardware Monitor, which is an open source library and application which you could utilize.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900