3

I have this code in 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 variable is null I don't know how to make an instance for it.

The other two variables in the constructor return 0 nothing. If I'm not using the ii variable the other two don't throw an error but don't return any values.

I'm using the openhardwaremonitor dll from http://code.google.com/p/open-hardware-monitor/downloads/detail?name=openhardwaremonitor-v0.4.0-beta.zip&can=2&q=

The c# dll is coming with the program it self.

So I added as reference the dll but I don't know how to make the code.

Could someone build for me just an example of the code according to my code here ? I tried to look in the openhwardwaremonitor site and source code there and didn't understand how to use it.

What else can I do ?

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1363119
  • 143
  • 1
  • 3
  • 13
  • Have you tried emailing the author? His/her email address is available at the link you posted. – Polyfun May 08 '12 at 09:45
  • ii is declared but not instanced, could that be the problem? I mean, you're missing the OpenHardwareMonitor.Hardware.ISensor ii = new Sensor(whatever); – CMPerez May 08 '12 at 09:56
  • could be Picacodigos and what the (whatever) sjould be ? couldnt find it. – user1363119 May 08 '12 at 10:29

1 Answers1

14

I've tested this code:

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;

        }
    }
}

The form has a multiline text control and a timer. Add a reference to OpenHardwareMonitorLib.dll.

You also need to request a higher execution level in the application, i.e. right click on the project, add a new manifest file item and declare

requestedExecutionLevel  level="highestAvailable" uiAccess="false"
Jesse
  • 8,605
  • 7
  • 47
  • 57
user2167340
  • 141
  • 1
  • 3