Windows下使用C#獲取CPU溫度信息


 一、測試環境:

  • VS2013
  • Windows10
  • Intel 8代酷睿(Laptop)

 

二、下載 Open hardware monitor

  Link: https://openhardwaremonitor.org/downloads/

  

三、創建項目

  以管理員權限啟動 VS2013 > 新建項目 > 左側選擇Visual C#,右側選擇 控制台應用程序(Console Application)> 確定

  

  創建好項目后,解壓下載的 Open hardware monitor 壓縮包文件

  然后在VS2013中右鍵單擊 引用 > 添加引用 > 左側選中瀏覽,右側點擊瀏覽,選中解壓出的 “OpenHardwareMonitorLib.dll”文件,完成引用文件的添加。

  

  

  

  復制以下代碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using OpenHardwareMonitor.Hardware;
 7 using System.Threading;
 8 namespace Get_CPU_Temp5
 9 {
10     class Program
11     {
12         public class UpdateVisitor : IVisitor
13         {
14             public void VisitComputer(IComputer computer)
15             {
16                 computer.Traverse(this);
17             }
18             public void VisitHardware(IHardware hardware)
19             {
20                 hardware.Update();
21                 foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
22             }
23             public void VisitSensor(ISensor sensor) { }
24             public void VisitParameter(IParameter parameter) { }
25         }
26         static void GetSystemInfo()
27         {
28             UpdateVisitor updateVisitor = new UpdateVisitor();
29             Computer computer = new Computer();
30             computer.Open();
31             computer.CPUEnabled = true;
32             computer.Accept(updateVisitor);
33             for (int i = 0; i < computer.Hardware.Length; i++)
34             {
35                 if (computer.Hardware[i].HardwareType == HardwareType.CPU)
36                 {
37                     for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
38                     {
39                         if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
40                             Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
41                     }
42                 }
43             }
44             computer.Close();
45         }
46         static void Main(string[] args)
47         {
48             while (true)
49             {
50                 GetSystemInfo();
51                 Thread.Sleep(5000);
52                 Console.WriteLine();
53                 Console.WriteLine();
54             }
55         }
56     }
57 }

 

  右鍵 > 添加 > 新建項,選中“應用程序清單文件”,確定。(生成app.manifest文件,如果文件已存在可忽略)

  

  

  

  將app.manifest文件中的 <requestedExecutionLevel level="asInvoker" uiAccess="false" /> 

  修改為 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

  

  

  嘗試運行,正常情況下每間隔5秒更新一次CPU溫度信息

  

  如果無信息顯示,請使用管理員權限運行VS2013程序

  本文為實踐筆記,參考帖:http://www.lattepanda.com/topic-f11t3004.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM