我在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 OpenHardwareMonitor.Hardware.HDD;
using OpenHardwareMonitor;
namespace OpenHardwareMonitor
{
public partial class Form1 : Form
{
OpenHardwareMonitor.Hardware.SensorValue sv;
OpenHardwareMonitor.Hardware.ISensor ii;
public Form1()
{
InitializeComponent();
string y = ii.Name;
sv = new Hardware.SensorValue();
DateTime dt = sv.Time;
float t = sv.Value;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}ii变量为空,我不知道如何为它创建实例。
构造函数中的另外两个变量不返回0。如果我没有使用ii变量,那么其他两个变量就不会抛出错误,也不会返回任何值。
我使用的是来自http://code.google.com/p/open-hardware-monitor/downloads/detail?name=openhardwaremonitor-v0.4.0-beta.zip&can=2&q=的openhardwaremonitor dll
c#动态链接库随程序本身一起提供。
所以我添加了dll作为引用,但我不知道如何编写代码。
有人能根据我这里的代码为我构建一个代码示例吗?我试着查看了openhwardwaremonitor站点和源代码,但不知道如何使用它。
我还能做什么?
谢谢。
发布于 2013-03-14 05:04:52
我已经测试了以下代码:
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 OpenHardwareMonitor;
using OpenHardwareMonitor.Hardware;
namespace CPUTemperatureMonitor
{
public partial class Form1 : Form
{
Computer thisComputer;
public Form1()
{
InitializeComponent();
thisComputer = new Computer() { CPUEnabled = true };
thisComputer.Open();
}
private void timer1_Tick(object sender, EventArgs e)
{
String temp = "";
foreach (var hardwareItem in thisComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
subHardware.Update();
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
temp += String.Format("{0} Temperature = {1}\r\n", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
}
}
}
}
textBox1.Text = temp;
}
}
}该窗体有一个多行文本控件和一个计时器。添加对OpenHardwareMonitorLib.dll的引用。
您还需要在应用程序中请求更高的执行级别,即右键单击项目,添加新的清单文件项并声明
requestedExecutionLevel level="highestAvailable" uiAccess="false"https://stackoverflow.com/questions/10495430
复制相似问题