3

我正在尝试使用 OpenHardwareMonitor 来读取我的硬件诊断信息。如果您曾经尝试过使用 OHM,那么您就会知道关于它的文档非常少,据我所知,没有其他开源库能够如此准确和健壮。

我已经能够从 CPU 获得负载和时钟速度,但不能获得温度。我也没有收到有关硬盘驱动器的任何信息(我最关心的是温度)。

这是我的实现,它应该返回它所拥有的所有内容,但到目前为止,我只获得了有关 RAM(无临时)、CPU(无临时)和 GPU 的有限信息。

CPU 温度是我要跟踪的最重要的统计数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using OpenHardwareMonitor.Hardware;

namespace OHMWrapper
{
    public class MySettings : ISettings
    {
        private IDictionary<string, string> settings = new Dictionary<string, string>();

        public MySettings(IDictionary<string, string> settings)
        {
            this.settings = settings;
        }

        public bool Contains(string name)
        {
            return settings.ContainsKey(name);
        }

        public string GetValue(string name, string value)
        {
            string result;
            if (settings.TryGetValue(name, out result))
                return result;
            else
                return value;
        }

        public void Remove(string name)
        {
            settings.Remove(name);
        }

        public void SetValue(string name, string value)
        {
            settings[name] = value;
        }
    }

    public class OHW
    {
        private static OHW m_Instance;
        public static OHW Instance
        {
            get
            {
                if (m_Instance == null)
                    m_Instance = new OHW();

                return m_Instance;
            }
        }
        private OHW()
        {
            m_Instance = this;
        }

        public void GetCPUTemp()
        {
            MySettings settings = new MySettings(new Dictionary<string, string>
            {
                { "/intelcpu/0/temperature/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9/+NX6N3/TJm9/5f/01fw1+fosnv+A/+OlfS37/jZ/s/Lpv9fff6Ml/NTef/yZPnozc5679b+i193//TQZ+/w2Dd+P9/sZeX/67v/GTf/b3iP3u4/ObBL//73+i+f039+D8Zk/+xz/e/P6beu2TQZju8yH8f6OgzcvPv/U3/Rb8+z/0f/9b/+yfaOn8079X6fr6Cws7ln/iHzNwflPv99/wyS/+xY4+v/evcJ+733+jJ5//Cw7/4ndy9Im3+U2e/Fbnrk31C93vrt/fyPvdb+N//hsF7/4/AQAA//9NLZZ8WAIAAA==" },
                { "/intelcpu/0/load/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9++mpwcv/md/9df89egZ/xX/ym/5y/4D37618Lv7ya//u+58+u+5d9/z7/5t/w9/6u5fP5bH/6av+eTkXyefXxp26ONaf/v/dG/sf39D/rvnv4e5vc/0IP56/waK/vuHzf5I38P8/tv+mv8Rbb9f0pwTF9/zr/1X9vP/8I//+/6Pf7Z30N+/zdf/HX29zd/859q4aCNP5b//U+U3/+7f+zXOjZwfqvDX/V7/o9/vPz+a1G/pv0f+fGlhfk7eZ//N3/0v28//5X0u/n8Cxq7+f1X/tHft20A5x8a/W5/02+BP36Nf+j/nv8XfzrT+c2//Ob4p3+vktvUhNs/+xcWikP6e/4T/5jS5M8/sL8vP/5ff49f/Ivl9//sHzv6PX/vXyG//9R/94/9HuZ34P/5vyC//3W/5e/1exa/k+Bw4bUBnU2bP4Xg/1bn0uafeTH6PatfKL//N3/0t2y/gG9+/8+IzqYNxmU+/+jwX7afY67/nwAAAP//GYSA31gCAAA=" },
            });

            Computer myComputer = new Computer(settings)
            {
                MainboardEnabled = true,
                CPUEnabled = true,
                RAMEnabled = true,
                GPUEnabled = true,
                FanControllerEnabled = true,
                HDDEnabled = true
            };

            myComputer.Open();

            foreach (var hardwareItem in myComputer.Hardware)
            {
                hardwareItem.Update();

                if (hardwareItem.SubHardware.Length > 0)
                {
                    foreach (IHardware subHardware in hardwareItem.SubHardware)
                    {
                        subHardware.Update();

                        foreach (var sensor in subHardware.Sensors)
                        {

                            Console.WriteLine(String.Format("{0} {1} = {2}", sensor.Name, sensor.Hardware, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value"));
                        }
                    }
                }
                else
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {

                        Console.WriteLine(String.Format("{0} {1} = {2}", sensor.Identifier, sensor.Hardware, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value"));
                    }
                }
            }
        }
    }
}

我一直在查看源代码,因为它是获取任何信息的唯一方法。这是一个缓慢的过程,但我非常感谢任何帮助。

OpenHardwareMonitor 源代码:http ://code.google.com/p/open-hardware-monitor/source/browse/

4

2 回答 2

3

我必须以管理权限运行此代码才能获得我想要的所有信息。我添加了一个清单文件,该文件需要代码与这些文件一起运行。感谢您所有有用的帖子。

于 2012-08-09T14:20:26.720 回答
1

我也被这个困住了,我只是想分享我的经验......

因此,以管理权限运行它是强制性的。尽管如此,从网络共享运行时它仍然可能无法正常工作。

设置的东西(MySettings)和cpu字符串不是必需的......我只是用过

myComputer = new Computer() { CPUEnabled = true };

就这样 ;)

干杯,斯蒂芬

于 2013-08-08T13:53:07.467 回答