WIN7 下用OpenHardwareMonitor 获取CPU温度代码直接可用

最近有软件需求要获取CPU 温度等硬件参数,WMI 研究了几天没有找到方法,最后在网上找到openHardwareMonitor 可以实现。

我最初参考是以下网站:https://www.cnblogs.com/chen110xi/p/6189499.html

不过里边的代码不能用:

1. 下载的OpenHardWareMonitor中包含了两个工程,一个是Lib,一个是监测软件。

2.按网站的说法,直接用Lib就可以了。而上边的代码中用到了app.config, 其他那个根本不用用在我们自己的工程中,那是给监测软件 用。UI 的一些数。

3.在我的电脑上,能取到CPU 的核心数和name, 就是取不到温度值。

4.后边再研究了下,发现里边自带的监测软件是可以用, 可以正常显示 温度等值。我就奇怪了,这个软件 能在我的电脑上正常工作,我的工程为什么不能呢。

5.进入到自带的Monitor 软件的mainForm中,发现有一个定时器,定时触发刷新CPU 的温度值。最后在源码中发现有这么一行(WmiProvider.cs中):

 public void Update() {
      foreach (IWmiObject instance in activeInstances)
        instance.Update();
    }

只有执行了这一行,温度值才会实现,不过for里边的update()函数是一个接口,看不到源码,有高手看可找到源码不,注释中写的:

namespace OpenHardwareMonitor.WMI {
   interface IWmiObject {
    // Both of these get exposed to WMI
    string Name { get; }
    
    string Identifier { get; }

    // Not exposed.
    void Update();
  }
}

6.发现了这个函数后加到我的主函数里,温度就出来了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

using OpenHardwareMonitor.Hardware;
using OpenHardwareMonitor.WMI;
using System.Reflection;
using OpenHardwareMonitor;
using OpenHardwareMonitor.GUI;

// 注意,我因为是用到了Monitor 原本的一些函数,所以在reference里,不仅要引用 :OpenHardwareMonitorLib, 还要引用OpenHardwareMonitor, 可以引用dll, exe,也可以引用工程。

 

public static string GetTemperatureProbe()
            {
                StringBuilder str = new StringBuilder();
#if true
                System.Version ver1 = Assembly.GetAssembly(typeof(Computer)).GetName().Version;
                System.Version ver2 = Assembly.GetExecutingAssembly().GetName().Version;
                //if (ver1 != ver2)
                //{
                //    MessageBox.Show(
                //      "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
                //      "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    Environment.Exit(0);
                //}

                PersistentSettings settings = new PersistentSettings();

                UpdateVisitor updateVisitor = new UpdateVisitor();

                //初始化config配置文件,详情见Bin目录Debug/Replase\OpenHardwareMonitor.config文件

                string configPath = "D:\\3_git\\utility-tools\\WMIVerify\\WindowsFormsApplication1\\resource\\OpenHardware\\Bin\\Debug\\OpenHardwareMonitor.config";
                settings.Load(configPath);

                //初始化Computer对象

                Computer computer= new Computer();

                WmiProvider wmiProvider = new WmiProvider(computer);

                //调用open方法

                computer.Open();

                //获取cpu温度: 

                computer.Accept(updateVisitor);

                if (wmiProvider != null)
                    wmiProvider.Update();

                for (int i = 0; i < computer.Hardware.Length; i++)
                {
                    str.Append(string.Format("\nHardware type: {0}\n", computer.Hardware[i].HardwareType.ToString()));

                    string unitStr = "";

                    //循环找到HardwareType为cpu
                    //if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                    //{
                        
                        for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                        {
                            //找到温度
                            str.Append(string.Format("Sensor type: {0} ", computer.Hardware[i].Sensors[j].SensorType.ToString()));

                            if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            {
                                unitStr = " ℃";
                            }
                            else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Load)
                            {
                                unitStr = " %";
                            }
                            else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Clock)
                            {
                                unitStr = " MHz";
                            }
                            else if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Voltage)
                            {
                                unitStr = " V";
                            }
                            else
                            {
                                unitStr = " ??";
                            }

                            //if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            //{
                                //获取到cpu核心
                                str.Append(string.Format("name: {0} ", computer.Hardware[i].Sensors[j].Name));
                                str.Append("\t");

                                //这里就是cpu温度了
                                str.Append(string.Format("value: {0} {1}", computer.Hardware[i].Sensors[j].Value.ToString(), unitStr));
                                str.Append("\n");
                            //}
                        }
                    //}
                }

#endif


                return str + "";
            }

7. 执行结果:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值