在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)
{
}
}
}它永远不会进入每一个跳过它的森林。我试了这么多示例,都搞不懂它是怎么工作的。我确信我的视频卡支持它,因为如果我运行的是原始的开放硬件监控程序ikts,它会显示所有的参数,比如温度和速度……
刚刚从程序官网下载了openhwardwaremonitor.dll文件:
http://openhardwaremonitor.org/downloads/动态链接库位于程序本身的目录中。
发布于 2012-08-01 05:50:19
我打开了ILSpy并查看了下载附带的示例exe,因为文档非常初级--通常是这样的:-)
我注意到在初始化Computer对象时,像GPUEnabled这样的布尔属性被设置为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窗体应用程序运行该代码时,我得到了当前温度(38摄氏度,这意味着我显然运行得不够努力!)
如果我不设置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事件就会触发一次,枚举硬件,并显示一个包含当前temp的消息框。这很容易成为一个标签,您甚至可以将Sensor存储在一个类级变量中,以避免每次都枚举IHardware集合。
希望这能有所帮助!
https://stackoverflow.com/questions/11749306
复制相似问题