Click here to Skip to main content
Rate this: bad
good
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 5:10am
Comments
Wes Aday at 10-Oct-12 11:16am
   
What problem?
Christopher Smit at 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 at 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 at 10-Oct-12 15:11pm
   
Is there really NO WAY to do this? I mean there must be something... Right?
OriginalGriff at 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 at 10-Oct-12 15:27pm
   
Well that sucks.. Would have been pretty epic being able to do that.. Thanks anyway :)
Christopher Smit at 15-Oct-12 14:59pm
   
I've done a little research.. How do programs like CPUID get the Temperature for the CPU?

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

  Print Answers RSS
0 OriginalGriff 250
1 Sergey Alexandrovich Kryukov 174
2 Rob Philpott 100
3 CHill60 70
4 Peter Leow 70
0 OriginalGriff 6,902
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,479
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
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