Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For my C# .Net Framework project I need to get 5 values:

1) CPU Load
2) CPU Temp
3) Current Voltage
4) VID Voltage
5) Current Frequency

After doing some research, I figured that the best way to obtain those values would be to use OpenHardwareMonitor or LibreHardwareMonitor (Git).

So, I installed LibreHardwareMonitor NuGet, it's added all references and the sample code from GitHub compiles, but the console closes immediately.

From the sample code below, you can tell that it outputs every single metric that it can obtain, while I need just those 5 values that I mentioned above.

If anyone has experience with LibreHardwareMonitor or OpenHardwareMonitor, I would truly appreciate it if you would tell me how to get just those values that I need.

public class UpdateVisitor : IVisitor
{
    public void VisitComputer(IComputer computer)
    {
        computer.Traverse(this);
    }
    public void VisitHardware(IHardware hardware)
    {
        hardware.Update();
        foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
    }
    public void VisitSensor(ISensor sensor) { }
    public void VisitParameter(IParameter parameter) { }
}

public void Monitor()
{
    Computer computer = new Computer
    {
        IsCpuEnabled = true,
        IsGpuEnabled = true,
        IsMemoryEnabled = true,
        IsMotherboardEnabled = true,
        IsControllerEnabled = true,
        IsNetworkEnabled = true,
        IsStorageEnabled = true
    };

    computer.Open();
    computer.Accept(new UpdateVisitor());

    foreach (IHardware hardware in computer.Hardware)
    {
        Console.WriteLine("Hardware: {0}", hardware.Name);
        
        foreach (IHardware subhardware in hardware.SubHardware)
        {
            Console.WriteLine("\tSubhardware: {0}", subhardware.Name);
            
            foreach (ISensor sensor in subhardware.Sensors)
            {
                Console.WriteLine("\t\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
            }
        }

        foreach (ISensor sensor in hardware.Sensors)
        {
            Console.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
        }
    }
    
    computer.Close();
}


What I have tried:

I tried WMI, but it doesn't work with modern CPUs, and can't get those values correctly.
Posted
Updated 19-Dec-21 2:25am
Comments
Richard MacCutchan 19-Dec-21 3:45am    
Are you running the program inside Visual Studio? If so you need to add the line Console.ReadLine(); at the end of the program. This will prevent it closing immediately.

If that is not the case then you need to provide more information.
John Bon Jovy 19-Dec-21 14:47pm    
Thanks! The problem is not closing by itself, but my inability to get values from the sensors.
I plainly want to get something like:
label1.Text = Voltage.ToString();
But regardless what I try, I can't manage to do that.

1 solution

ISensor supports a property SensorType SensorType which is defined in ISensor like this:

public enum SensorType
{
    Voltage, // V
    Current, // A
    Power, // W
    Clock, // MHz
    Temperature, // °C
    Load, // %
    Frequency, // Hz
    Fan, // RPM
    Flow, // L/h
    Control, // %
    Level, // %
    Factor, // 1
    Data, // GB = 2^30 Bytes
    SmallData, // MB = 2^20 Bytes
    Throughput, // B/s
    TimeSpan, // Seconds
}


This makes it easy to filter out your desired values e.g. here

foreach (ISensor sensor in hardware.Sensors)
{
   if (sensor.SensorType == TODO By You: thisorthat) // <<<< Here your desired values *1)
   {
      Console.WriteLine("\tSensor: {0}, value: {1}", sensor.Name, sensor.Value);
   }
}


*1) Either by an ugly "if ((sensor.SensorType == isThis) || (sensor.SensorType == isThat) || ...) or you put your desired values in a e.g. HashSet to make it more elegant ;)

I hope it helps.
 
Share this answer
 
v2
Comments
0x01AA 20-Dec-21 4:25am    
Sometething like this should work (sorry, formatting not survived here):
foreach (ISensor sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Voltage)
{
// sensor.Value holds the Voltage.
// Continue processing this value according to your needs
}
else if (sensor.SensorType == SensorType.Temperature)
{
// sensor.Value holds the Temperature
}
// and so on ...
}
Richard MacCutchan 20-Dec-21 4:38am    
Except that those locally scoped variables will not survive the foreach loop.
John Bon Jovy 20-Dec-21 4:51am    
What do you mean?
Richard MacCutchan 20-Dec-21 4:57am    
My comment was directed at 0x01AA. However, declaring a variable within the scope of a for loop means that when the loop ends the variable will no longer exist.
0x01AA 20-Dec-21 4:57am    
Yes you are right, but the above is only for a first test.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900