Get CPU temperature in CMD/POWER Shell

后端 未结 5 1998
暖寄归人
暖寄归人 2020-12-18 00:48

In my computer I am trying to get the CPU temperature. Searching on StackOverflow I found this:

C:\\WINDOWS\\system32>wmic /namespace:\\\\root\\wmi PATH M         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-18 01:16

    You can get the CPU temp in both WMI and Open Hardware Monitor way.

    Open Hardware Monitor:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using OpenHardwareMonitor.Hardware;
    namespace Get_CPU_Temp5
    {
       class Program
       {
           public class UpdateVisitor : IVisitor
           {
               public void VisitComputer(IComputer computer)
               {
                   computer.Traverse(this);
               }
               public void VisitHardware(IHardware hardware)
               {
                   hardware.Update();
                   foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
               }
               public void VisitSensor(ISensor sensor) { }
               public void VisitParameter(IParameter parameter) { }
           }
           static void GetSystemInfo()
           {
               UpdateVisitor updateVisitor = new UpdateVisitor();
               Computer computer = new Computer();
               computer.Open();
               computer.CPUEnabled = true;
               computer.Accept(updateVisitor);
               for (int i = 0; i < computer.Hardware.Length; i++)
               {
                   if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                   {
                       for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                       {
                           if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                   Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                       }
                   }
               }
               computer.Close();
           }
           static void Main(string[] args)
           {
               while (true)
               {
                   GetSystemInfo();
               }
           }
       }
    }
    

    WMI:

    using System;
    using System.Diagnostics;
    using System.Management;
    class Program
    {
       static void Main(string[] args)
       {
           Double CPUtprt = 0;
           System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(@"root\WMI", "Select * From MSAcpi_ThermalZoneTemperature");
           foreach (System.Management.ManagementObject mo in mos.Get())
           {
               CPUtprt = Convert.ToDouble(Convert.ToDouble(mo.GetPropertyValue("CurrentTemperature").ToString()) - 2732) / 10;
              Console.WriteLine("CPU temp : " + CPUtprt.ToString() + " °C");
           }
       }
    }
    

    I found a nice tutorial here, I get the CPU temp successfully.

    http://www.lattepanda.com/topic-f11t3004.html

提交回复
热议问题