在 form1 中我做了:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using OpenHardwareMonitor.Hardware;
namespace NvidiaTemp
{
public partial class Form1 : Form
{
Computer computer = new Computer();
public Form1()
{
InitializeComponent();
computer.Open();
var temps = new List<decimal>();
foreach (var hardware in computer.Hardware)
{
if (hardware.HardwareType != HardwareType.CPU)
continue;
hardware.Update();
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType != SensorType.Temperature)
{
if (sensor.Value != null)
temps.Add((decimal)sensor.Value);
}
}
}
foreach (decimal temp in temps)
{
Console.WriteLine(temp);
MessageBox.Show(temp.ToString());
}
Console.ReadLine();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
它永远不会进入 foreach 跳过它。 尝试了这么多示例无法弄清楚它是如何工作的。我确信我的显卡支持它,因为如果我运行 origiran openhardwaremonitor 程序 ikts 如何处理所有参数,如温度和速度...
刚刚从官方程序网站下载了openhwardwaremonitor.dll文件:
http://openhardwaremonitor.org/downloads/ 该dll位于程序本身的目录中。
最佳答案
我爆发了ILSpy并查看了下载附带的示例 exe,因为文档非常简陋 - 通常就是这种情况:-)
我注意到,当 Computer
对象初始化时,GPUEnabled
等 bool 属性被设置为 true。
所以...
Computer myComputer = new Computer();
myComputer.Open();
myComputer.GPUEnabled = true; //This is the line you are missing.
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value));
}
}
}
}
当我从计算机上的 Windows 窗体应用程序运行该代码时,我得到当前温度(38C,这意味着我显然运行得不够努力!)
如果我不设置 GPUEnabled,我会得到与您完全相同的结果 - IHardware
集合中没有任何项目。
更新:
要回答您在评论中提出的其他问题,下面的示例应该适合您:
Timer timer;
Computer myComputer;
public Form1()
{
InitializeComponent();
myComputer = new Computer();
myComputer.Open();
myComputer.GPUEnabled = true;
timer = new Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value));
}
}
}
}
}
这里我们有一个 Windows.Forms.Timer
和 Computer
类作为类级别变量,我们在构造函数中初始化它们。每 5 秒,tick
事件就会触发,枚举硬件,并显示一个包含当前温度的消息框。这很容易成为一个标签,您甚至可以存储 Sensor
放在类级别变量中,以避免每次都枚举 IHardware
集合。
希望这有帮助!
关于c# - 我尝试使用 OpenHardwareMonitor 源代码,但从未获得我的显卡温度,这可能是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11749306/