2012-05-08 52 views
3

我有這樣的代碼在Form1中:如何在c#中使用開放硬件監視器源代碼?我想任何事情不工作

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變量,其他兩個不會拋出錯誤,但不返回任何值。

我使用的DLL openhardwaremonitor從http://code.google.com/p/open-hardware-monitor/downloads/detail?name=openhardwaremonitor-v0.4.0-beta.zip&can=2&q=

C#的DLL與程序它的自我到來。

因此,我添加了作爲參考的DLL,但我不知道如何使代碼。

有人能爲我構建一個根據我的代碼在這裏的代碼的例子嗎? 我試圖在openhwardwaremonitor網站和源代碼看看,並不明白如何使用它。

我還能做什麼?

謝謝。

+0

您是否嘗試過電子郵件的作者?他/她的電子郵件地址可在您發佈的鏈接中找到。 – Polyfun

+0

ii被宣佈但沒有實例,這可能是問題嗎?我的意思是,你錯過了OpenHardwareMonitor.Hardware.ISensor ii =新的傳感器(無論); – CMPerez

+0

可能是Picacodigos和什麼(什麼)sjould?無法找到它。 – user1363119

回答

9

我已經測試此代碼:

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" 
+0

完美的作品。非常感謝 – Lambda

相關問題