作用缘由 想通过命令来获取到当前硬件状态后通过MQTT发送到相应服务器。相应的也能学习到C#获取方法,刚开始查询到的是使用 OpenHardwareMonitor 获取状态,其实这个软件也一直在使用,而且也能通过tcp端口查询到win环境下的硬件状态。不过,尝试之后发现当前net6.0版本并不适合,或者说这个如今已经不维护了,这也是在一片评论中看到,并且也提到了 LibreHardwareMonitor 这个库。不过之前的也是有参考价值。
OpenHardwareMonitor 方法跟步骤 C# 去获取电脑相关的基础信息,还是需要借助 外部的库,我这边尝试了自己去实现它
网上有一些信息,但不太完整,都比较零碎,这边尽量将代码完整的去展示出来
OpenHardwareMonitor获取CPU的温度和频率需要管理员权限 在没有开权限的时候就是无法使用
首先添加相关应用的dll文件 引用–>添加引用—>浏览(选择文件)–>确定
添加头文件 1 2 3 using System.IO;using OpenHardwareMonitor.Hardware;using System.Threading;
代码中注释掉的部分是循环显示的一个循环逻辑
代码的功能可以将主板的名称显示出来,还有将第一个CPU的情况显示,可以根据实际情况进行修改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Diagnostics;using System.IO;using OpenHardwareMonitor.Hardware;using System.Threading;namespace cpuGpu { public partial class Form1 : Form { 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 ) { } } public Form1 ( ) { InitializeComponent(); } private void button2_Click (object sender, EventArgs e ) { UpdateVisitor updateVisitor = new UpdateVisitor(); Computer myComputer = new Computer(); myComputer.Open(); myComputer.MainboardEnabled = true ; myComputer.CPUEnabled = true ; myComputer.RAMEnabled = true ; myComputer.GPUEnabled = true ; myComputer.FanControllerEnabled = true ; myComputer.HDDEnabled = true ; do { myComputer.Accept(updateVisitor); Console.Clear(); string BroadName = myComputer.Hardware[0 ].Name.ToString(); string CPUdata = myComputer.Hardware[1 ].Sensors[0 ].Value.ToString(); System.Threading.Thread.Sleep(1000 ); } while (true ); } } }
上述OpenHardwareMonitor方法被弃用了
LibreHardwareMonitorLib 方法完成 库添加方法跟前面一样
win10温度获取的方法,在win11已经不行了 下面是win11获取温度代码 先安装包 LibreHardwareMonitorLib 用管理员身份运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using LibreHardwareMonitor.Hardware;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace 硬件温度2 { 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 ) { } } internal class Program { public static void Monitor ( ) { Computer computer = new Computer { IsCpuEnabled = true , IsGpuEnabled = true , IsMemoryEnabled = true , IsMotherboardEnabled = true , IsControllerEnabled = true , IsNetworkEnabled = true , IsStorageEnabled = true }; computer.Open(); computer.Accept(new UpdateVisitor()); foreach (IHardware hardware in computer.Hardware) { Console.WriteLine("Hardware: {0}" , hardware.Name); foreach (IHardware subhardware in hardware.SubHardware) { Console.WriteLine("\tSubhardware: {0}" , subhardware.Name); foreach (ISensor sensor in subhardware.Sensors) { Console.WriteLine("\t\tSensor: {0}, value: {1}" , sensor.Name, sensor.Value); } } foreach (ISensor sensor in hardware.Sensors) { Console.WriteLine("\tSensor: {0}, value: {1}" , sensor.Name, sensor.Value); } } computer.Close(); } static void Main (string [] args ) { Monitor(); Console.ReadKey(); } } }
自己调整的方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 public class HardwareMonitor { 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 ) { } } public static void HardwareMonitorRead ( ) { Computer computer = new () { IsCpuEnabled = true , IsGpuEnabled = true , IsMemoryEnabled = true , IsMotherboardEnabled = true , IsControllerEnabled = true , IsStorageEnabled = true }; computer.Open(); computer.Accept(new UpdateVisitor()); foreach (var hardwareItem in computer.Hardware) { Debug.WriteLine(hardwareItem.HardwareType + "型号是:" + hardwareItem.Name); foreach (var sensor in hardwareItem.Sensors) { Debug.WriteLine(sensor.Name + "的" + sensor.SensorType + "是" + sensor.Value); } } computer.Close(); }
相关链接(侵删)
(就是参照的这个)win11 c#硬件温度获取
C#使用 OpenHardwareMonitor 获取CPU或显卡温度、使用率、时钟频率相关方式
How can I get the CPU temperature?
C#如何获取电脑CPU温度,硬盘温度,主板温度,CPU转速
=================我是分割线=================
欢迎到公众号来唠嗑: