-2

So I need to get the CPU temp for my program, and I am using the code below.

So, to get the CPU temp I used this code:

static void Main(string[] args)
{
    try
    {
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("root\\WMI",
            "SELECT * FROM MSAcpi_ThermalZoneTemperature");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("MSAcpi_ThermalZoneTemperature instance");
            Console.WriteLine("-----------------------------------");
            Console.WriteLine("CurrentTemperature: {0}", (queryObj["CurrentTemperature"]));
            Console.WriteLine(queryObj);
            Console.ReadKey();
        }
    }
    catch (ManagementException e)
    {
        Console.Write(e);
        Console.ReadKey();
    }
}

I need to add 2732 and divide it by 10 to get the value in celsius, but I can't find the variable to divide?

Any help is appreciated.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Cratherton
  • 17
  • 1
  • 9

2 Answers2

0

That's your temperature: queryObj["CurrentTemperature"].

This is of type object, so you first need to cast it to int, double or float, depending on the actual type. You can get the type by calling GetType() on the object.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
0

store queryObj["CurrentTemperature"] in a variable

var temp = queryObj["CurrentTemperature"];

cast it to an int then do your addition and division.

Jay
  • 2,077
  • 5
  • 24
  • 39