首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在c#中使用开放硬件监视器源代码?我试过任何东西都不起作用

如何在c#中使用开放硬件监视器源代码?我试过任何东西都不起作用
EN

Stack Overflow用户
提问于 2012-05-08 16:49:22
回答 1查看 12.3K关注 0票数 3

我在Form1中有这样的代码:

代码语言:javascript
复制
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站点和源代码,但不知道如何使用它。

我还能做什么?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-03-14 05:04:52

我已经测试了以下代码:

代码语言:javascript
复制
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的引用。

您还需要在应用程序中请求更高的执行级别,即右键单击项目,添加新的清单文件项并声明

代码语言:javascript
复制
requestedExecutionLevel  level="highestAvailable" uiAccess="false"
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10495430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档