Click here to Skip to main content
12,620,840 members (35,822 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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?

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 10-Oct-12 6:10am
Comments
Wes Aday 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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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."
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161128.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100