get system cpu temperature in csharp

One way to get the system CPU temperature in C# is by using the ManagementObjectSearcher class from the System.Management namespace.

Here's an example code snippet that shows how to get the CPU temperature:

main.cs
using System.Management;

public static float GetCpuTemperature()
{
    float temperature = 0;
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\OpenHardwareMonitor", "SELECT Value FROM Sensor WHERE Identifier = '/intelcpu/0/temperature/0'");
    foreach (ManagementObject obj in searcher.Get())
    {
        temperature = Convert.ToSingle(obj["Value"]);
        temperature = temperature / 1000;
    }
    return temperature;
}
456 chars
14 lines

This code uses the Open Hardware Monitor library to get the CPU temperature. It searches for the temperature sensor with the identifier "/intelcpu/0/temperature/0" and returns the value in Celsius degrees. Note that you'll need to add a reference to the System.Management and OpenHardwareMonitorLib assemblies to use this code.

gistlibby LogSnag