-1

I want to know how to access to system information like CPU-GPU-RAM usage-temp-frequency and...

is there an easy way to do this? if not, can I use other softwares like HWiNFO to extract this informations ? Like An CMD App That Can Run from CMD And Return CPU Temp or Else.

News :

OK . I Found This But its throwing an exception that I don't Know What it is ! Here is Code From performancepsu.com :

using OpenHardwareMonitor.Hardware;

namespace Monitor

{ class Program {

    // CPU Temp
    static float cpuTemp;
    // CPU Usage
    static float cpuUsage;
    // CPU Power Draw (Package)
    static float cpuPowerDrawPackage;
    // CPU Frequency
    static float cpuFrequency;
    // GPU Temperature
    static float gpuTemp;
    // GPU Usage
    static float gpuUsage;
    // GPU Core Frequency
    static float gpuCoreFrequency;
    // GPU Memory Frequency
    static float gpuMemoryFrequency;

    /**
     * Init OpenHardwareMonitor.dll Computer Object
     **/

    static Computer c = new Computer()
    {
        GPUEnabled = true,
        CPUEnabled = true,
        //RAMEnabled = true, // uncomment for RAM reports
        //MainboardEnabled = true, // uncomment for Motherboard reports
        //FanControllerEnabled = true, // uncomment for FAN Reports
        //HDDEnabled = true, // uncomment for HDD Report
    };

    /**
     * Pulls data from OHM
     **/

    static void ReportSystemInfo()
    {
        foreach (var hardware in c.Hardware)
        {

            if (hardware.HardwareType == HardwareType.CPU)
            {
                // only fire the update when found
                hardware.Update();

                // loop through the data
                foreach (var sensor in hardware.Sensors)
                    if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("CPU Package"))
                    {
                        // store
                        cpuTemp = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("cpuTemp: " + sensor.Value.GetValueOrDefault());

                    }
                    else if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("CPU Total"))
                    {
                        // store
                        cpuUsage = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("cpuUsage: " + sensor.Value.GetValueOrDefault());

                    }
                    else if (sensor.SensorType == SensorType.Power && sensor.Name.Contains("CPU Package"))
                    {
                        // store
                        cpuPowerDrawPackage = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("CPU Power Draw - Package: " + sensor.Value.GetValueOrDefault());


                    }
                    else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("CPU Core #1"))
                    {
                        // store
                        cpuFrequency = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("cpuFrequency: " + sensor.Value.GetValueOrDefault());
                    }
            }


            // Targets AMD & Nvidia GPUS
            if (hardware.HardwareType == HardwareType.GpuAti || hardware.HardwareType == HardwareType.GpuNvidia)
            {
                // only fire the update when found
                hardware.Update();

                // loop through the data
                foreach (var sensor in hardware.Sensors)
                    if (sensor.SensorType == SensorType.Temperature && sensor.Name.Contains("GPU Core"))
                    {
                        // store
                        gpuTemp = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("gpuTemp: " + sensor.Value.GetValueOrDefault());
                    }
                    else if (sensor.SensorType == SensorType.Load && sensor.Name.Contains("GPU Core"))
                    {
                        // store
                        gpuUsage = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("gpuUsage: " + sensor.Value.GetValueOrDefault());
                    }
                    else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Core"))
                    {
                        // store
                        gpuCoreFrequency = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("gpuCoreFrequency: " + sensor.Value.GetValueOrDefault());
                    }
                    else if (sensor.SensorType == SensorType.Clock && sensor.Name.Contains("GPU Memory"))
                    {
                        // store
                        gpuMemoryFrequency = sensor.Value.GetValueOrDefault();
                        // print to console
                        System.Diagnostics.Debug.WriteLine("gpuMemoryFrequency: " + sensor.Value.GetValueOrDefault());
                    }

            }

            // ... you can access any other system information you want here

        }
    }

    static void Main(string[] args)
    {
        c.Open();

        // loop
        while (true)
        {
            ReportSystemInfo();
        }
    }
}

}

And the Error is :

Unhandled exception. System.MissingMethodException: Method not found: 'System.Threading.Mutex System.Threading.Mutex.OpenExisting(System.String, System.Security.AccessControl.MutexRights)'. at OpenHardwareMonitor.Hardware.Ring0.Open() at OpenHardwareMonitor.Hardware.Computer.Open() at Monitor.Program.Main(String[] args) in D:\VisualStudio\Monitor_Test\Monitor_Test\Program.cs:line 136

What Should I do ?

ALI
  • 29
  • 3
  • Does this answer your question? [How can I get the CPU temperature?](https://stackoverflow.com/questions/1195112/how-can-i-get-the-cpu-temperature) – Rahatur Jan 04 '23 at 14:44
  • [This](https://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I) is a very old article but it seems like the starting point for your research should be [System.Management](https://learn.microsoft.com/en-us/dotnet/api/system.management?view=dotnet-plat-ext-7.0) – Narish Jan 04 '23 at 14:48
  • Does this answer your question? [C# CPU and GPU Temp](https://stackoverflow.com/questions/29607595/c-sharp-cpu-and-gpu-temp) – anymous Jan 04 '23 at 15:00

2 Answers2

1

OK . After a Long Time I Found the Solution. First Download .NET Framework SDK (I used 4.8) . Then Install it. Set Target Framework of Project to 4.8 in .csproj file :

<TargetFramework>net48</TargetFramework>

and Remove This Tags :

<ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

then add a manifest file to project and replace first tag with second :

<requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

THE END !

Output is :

CPU Usage : 2 %

CPU Temp : 46 °C

CPU Clock : 3520 MHz

GPU Usage : 0 %

GPU Temp : 36 °C

GPU Clock : 405 MHz

RAM Usage : 84 %

ALI
  • 29
  • 3
0

Use System.Management DLL to Query WMI. look at this article :

https://ourcodeworld.com/articles/read/335/how-to-retrieve-the-cpu-s-temperature-with-c-in-winforms