一、测试环境:
- 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