C#OpenHardwareMonitorlib.dll可以';t对不同的核心使用索引

本文关键字:核心 索引 可以 dll C#OpenHardwareMonitorlib | 更新日期: 2023-09-27 18:27:52

我正在使用OpenHardwareMonitorLib.dll为自己制作一个程序,但我希望能够查看每个核心的负载和温度。单击图像链接查看下面的WMI。

图像

正如你所看到的,每个实例,即我的英特尔cpu的每个核心都有4个核心,所以有4个实例。。。好吧,5由于总负载,我希望能够使用索引来检索每个实例,但我很难做到这一点,因为.dll文件只有在我正确的情况下才有公共get访问器,如果我错了,请更正我。

我试着设置了传感器。变量的索引,但错误显示为只读,因此yano。。。我试过if控制结构,例如if传感器。索引==1。。。但它不起作用——它会自动抓取最后一个索引。

下面是我到目前为止所编写的代码。请关注这个问题,而不是我多余的代码我只是一个学生。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using OpenHardwareMonitor.Hardware;
namespace Hardware
{
    public partial class Temps : Form
    {
    Timer timer;
    private double ramusedpt;
    private double ramusedptx;
    private double systemram;
    int xaxis;
    Computer myComputer;
    ISensor NvdGPUTemperatureSensor, ATIGPUTemperatureSensor, CPUTemperatureSensor, NvdGPUFanSpeed, ATIGPUFanSpeed,
        NvdGPUFanCtrl, GPUShaderSpeed, GPUMemoryLoad, CPUFanSpeed, RamUsage, RamUsed, CPULoadSensor, CPUCoreTest;
    public Temps()
    {
        InitializeComponent();
        myComputer = new Computer();
        myComputer.Open();
        myComputer.GPUEnabled = true;
        myComputer.CPUEnabled = true;
        myComputer.FanControllerEnabled = true;
        myComputer.MainboardEnabled = true;
        myComputer.RAMEnabled = true;
        myComputer.HDDEnabled = true;
        string ram = Getcomponent("Win32_OperatingSystem", "TotalVisibleMemorySize");
        systemram = double.Parse(ram);
        systemram = Math.Round(systemram / 1048576);
        foreach (var hardwareItem in myComputer.Hardware)
        {
            foreach (var subhardware in hardwareItem.SubHardware)
            {
                subhardware.Update();
                if (subhardware.Sensors.Length > 0)
                {
                    foreach (var sensor in subhardware.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Fan)
                        {
                            CPUFanSpeed = sensor;
                        }
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Temperature)
                    {
                        NvdGPUTemperatureSensor = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.GpuAti)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Temperature)
                    {
                        ATIGPUTemperatureSensor  = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.CPU)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Temperature)
                    {
                        CPUTemperatureSensor = sensor;
                    }
                }
            }
            // GPU Fan Sensor
            if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Fan)
                    {
                        NvdGPUFanSpeed = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.GpuAti)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Fan)
                    {
                        ATIGPUFanSpeed = sensor;
                    }
                }
            }
            // GPU Fan Control
            if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Control)
                    {
                        NvdGPUFanCtrl = sensor;
                    }
                }
            }
            //
            if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Clock)
                    {
                        GPUShaderSpeed = sensor;
                    }
                }
            }
            if(hardwareItem.HardwareType == HardwareType.GpuNvidia)
            {
                foreach(var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Load)
                    {
                        GPUMemoryLoad = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.RAM)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Data)
                    {
                        RamUsage = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.RAM)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Factor)
                    {
                        RamUsed = sensor;
                    }
                }
            }
            if (hardwareItem.HardwareType == HardwareType.CPU)
            {
                foreach (var sensor in hardwareItem.Sensors)
                {
                    if (sensor.SensorType == SensorType.Load)
                    {
                        CPULoadSensor = sensor;
                    }
                }
            }
        }
        timer = new Timer();
        timer.Interval = 500;
        timer.Tick += new EventHandler(timer1_Tick);
        timer.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        string gpu = Getcomponent("Win32_VideoController", "Name");
        GPUlabel.Text = gpu;
        gpuFanLabel.Text = Convert.ToString(NvdGPUFanSpeed.Identifier);
        cpulabel.Text = Convert.ToString(CPUTemperatureSensor.Name);
        //GPU Temp sensor
        if (NvdGPUTemperatureSensor != null && GPUlabel.Text.Contains("NVIDIA"))
        {
            NvdGPUTemperatureSensor.Hardware.Update();
            gpuCurrent.Text = Convert.ToString(NvdGPUTemperatureSensor.Value);
            gpuMax.Text = Convert.ToString(NvdGPUTemperatureSensor.Max);
        }
        else if (ATIGPUTemperatureSensor != null && GPUlabel.Text.Contains("AMD"))
        {
            ATIGPUTemperatureSensor.Hardware.Update();
            gpuCurrent.Text = Convert.ToString(ATIGPUTemperatureSensor.Value);
            gpuMax.Text = Convert.ToString(ATIGPUTemperatureSensor.Max);
        }
        else
        {
            gpuCurrent.Text = "N/A";
            gpuMax.Text = "N/A";
        }
        // CPU temp sensor
        if (CPUTemperatureSensor != null)
        {
            CPUTemperatureSensor.Hardware.Update();
            cputxtval.Text = Convert.ToString(CPUTemperatureSensor.Value);
            cputxtmax.Text = Convert.ToString(CPUTemperatureSensor.Max);
        }
        else
        {
            cputxtval.Text = "N/A";
            cputxtmax.Text = "N/A";
        }
        // GPU Fan Speed
        if (NvdGPUFanSpeed != null && GPUlabel.Text.Contains("NVIDIA"))
        {
            NvdGPUFanSpeed.Hardware.Update();
            gpuFanTxt.Text = Convert.ToString(NvdGPUFanSpeed.Value);
            gpuFanMax.Text = Convert.ToString(NvdGPUFanSpeed.Max);
        }
        else if (ATIGPUFanSpeed != null && GPUlabel.Text.Contains("AMD"))
        {
            ATIGPUFanSpeed.Hardware.Update();
            gpuFanTxt.Text = Convert.ToString(ATIGPUFanSpeed.Value);
            gpuFanMax.Text = Convert.ToString(ATIGPUFanSpeed.Max);
        }
        else
        {
            gpuFanTxt.Text = "N/A";
            gpuFanMax.Text = "N/A";
        }
        // GPU Fan Speed Control
        if (NvdGPUFanCtrl != null)
        {
            NvdGPUFanCtrl.Hardware.Update();
            gpufanpb.Value = ((int)NvdGPUFanCtrl.Value);
        }
        else
        {
            MessageBox.Show("Failed to grab Fan Control value.", "Failure");
        }
        // GPU Shader Speed
        if (GPUShaderSpeed != null )
        {
            GPUShaderSpeed.Hardware.Update();
            gpuShaderVal.Text = Convert.ToString(Convert.ToInt16(GPUShaderSpeed.Value));
            gpuShaderMax.Text = Convert.ToString(Convert.ToInt16(GPUShaderSpeed.Max));
        }
        else
        {
            gpuShaderVal.Text = "N/A";
            gpuShaderMax.Text = "N/A";
        }
        // GPU Memory Load
        if (GPUMemoryLoad != null)
        {
            GPUMemoryLoad.Hardware.Update();
            gpuTestVal.Text = Convert.ToString(Math.Round((double)GPUMemoryLoad.Value, 1));
            gpuTestMax.Text = Convert.ToString(Math.Round((double)GPUMemoryLoad.Max, 1));
            gpuMempb.Value = ((int)GPUMemoryLoad.Value);
        }
        else
        {
            gpuTestVal.Text = "N/A";
            gpuTestMax.Text = "N/A";
        }
        // CPU Fan Speed
        if (CPUFanSpeed != null)
        {
            CPUFanSpeed.Hardware.Update();
            cpuFanVal.Text = Convert.ToString((int)(float)CPUFanSpeed.Value);
            cpuFanMax.Text = Convert.ToString((int)(float)CPUFanSpeed.Max);
        }
        else
        {
            cpuFanVal.Text = "N/A";
            cpuFanMax.Text = "N/A";
        }
        // RAM Usage
        if (RamUsage != null)
        {
            RamUsage.Hardware.Update();
            ramVal.Text = Convert.ToString(Math.Round((double)RamUsage.Value, 2));
            ramValMax.Text = Convert.ToString(Math.Round((double)RamUsage.Max, 2));
            ramusedpt = Math.Round((double)RamUsage.Value, 2);
            ramusedptx = Math.Round((Double)RamUsage.Max, 2);
        }
        else
        {
            ramVal.Text = "N/A";
            ramValMax.Text = "N/A";
        }
        // Ram Used
        if (RamUsage != null)
        {
            usedRamVal.Text = Convert.ToString(systemram - ramusedpt);
            usedRamMax.Text = Convert.ToString(systemram - ramusedptx);
        }
        else
        {   
            usedRamVal.Text = "N/A";
            usedRamMax.Text = "N/A";
        }
        if (CPUCoreTest != null)
        {
            CPUCoreTest.Hardware.Update();
            cpucore2txt.Text = Convert.ToString(CPUCoreTest.Value);
        }
        else
        {
            cpucore2txt.Text = "N/A";
        }
        try
        {
            chart1.Series[0].Points.AddXY(xaxis++, CPUTemperatureSensor.Value);
            chart1.Series[1].Points.AddXY(xaxis, NvdGPUTemperatureSensor.Value);
            chart1.Series[2].Points.AddXY(xaxis, ramusedpt);
        }
        catch
        {
        }
    }
    private static string Getcomponent(string hwclass, string syntax)
    {
        StringBuilder comp = new StringBuilder();
        ManagementObjectSearcher mos = new ManagementObjectSearcher("root''CIMV2", "SELECT * FROM " + hwclass);
        foreach (ManagementObject mj in mos.Get())
            comp.Append(Convert.ToString(mj[syntax]));
        return comp.ToString();
    }
}

}

http://openhardwaremonitor.org/wordpress/wp-content/uploads/2011/04/OpenHardwareMonitor-WMI.pdf

感谢的任何帮助

C#OpenHardwareMonitorlib.dll可以';t对不同的核心使用索引

我设法使if控制结构正常工作。张贴因为它可能有助于人们在未来

if (hardwareItem.HardwareType == HardwareType.CPU)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Load)
                        {
                            if (sensor.Index == 1)
                            {
                                CPUCoreTest2 = sensor;
                            }
                        }
                    }
                }

基本上,0的索引是cpu总数。1是核心1。2是核心2。3是核心3。4 iscore 4.etc…:)