获取“System.ArgumentOutOfRangeException”在C#中使用WMI时 [英] Getting "System.ArgumentOutOfRangeException" when working with WMI in C#

查看:64
本文介绍了获取“System.ArgumentOutOfRangeException”在C#中使用WMI时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以一种形式读出CPU温度和负载。我已经设法让这与CPU负载一起工作,但是当我试图获得CPU温度的结果并且我一直收到行上的System.ArgumentOutOfRangeException错误时我被卡住了:ManagementObject temperature = new ManagementObject(MSAcpi_ThermalZoneTemperature );打开表单时。



这是我的代码:

I would like to have a read out of the CPU temperature and load in one form. I have managed to get this to work with the CPU load, however I am stuck when trying to get results with the CPU temperature and I keep receiving a "System.ArgumentOutOfRangeException" error on the line: "ManagementObject temperature = new ManagementObject("MSAcpi_ThermalZoneTemperature");" When opening the form.

here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;




namespace CPUForm
{
    public class CPUPage : Form
    {
        public CPUPage()
        {
            InitializeComponent();
            timer1.Enabled = true;
            timer1.Interval = 1000;
           CPULOAD();
            CPUTemperature();
        }

      public void CPULOAD()
        {
            ulong CPULOAD;
            ManagementObject processor = new ManagementObject(
            "Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
            processor.Get();
            CPULOAD = (ulong)processor.Properties["PercentProcessorTime"].Value;
            label2.Text = CPULOAD.ToString() + "%";

        } 
        public void CPUTemperature()
       {
           ulong CPUTemperature;
           ManagementObject temperature = new ManagementObject(
          "MSAcpi_ThermalZoneTemperature");
           temperature.Get();
           CPUTemperature = (ulong)temperature.Properties["CurrentTemperature"].Value;
           label3.Text = CPUTemperature.ToString();
       }
        private void timer1_Tick(object sender, EventArgs e)
        {

            CPULOAD();
            CPUTemperature();
        }
        private Label label1;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(25, 164);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(61, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "CPU LOAD";
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Window;
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.label2.Location = new System.Drawing.Point(28, 31);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(32, 6, 0, 0);
            this.label2.Size = new System.Drawing.Size(109, 121);
            this.label2.TabIndex = 2;
            this.label2.Text = "0%";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);
            // 
            // label3
            // 
            this.label3.BackColor = System.Drawing.SystemColors.Window;
            this.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.label3.Location = new System.Drawing.Point(244, 31);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(32, 6, 0, 0);
            this.label3.Size = new System.Drawing.Size(201, 146);
            this.label3.TabIndex = 3;
            // 
            // CPUPage
            // 
            this.ClientSize = new System.Drawing.Size(624, 266);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "CPUPage";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private Label label2;
        private Timer timer1;
        private IContainer components;
        private Label label3;

        private void timer1_Tick_1(object sender, EventArgs e)
        {
           CPULOAD();
            CPUTemperature();
        }
    }
}

推荐答案

两个问题。



首先,MSAcpi_ThermalZoneTemperature类位于WMIroot\WMI命名空间中。您正在使用root\cimv2命名空间,因为您没有在WMI代码中指定一个。



其次,并非所有主板都支持报告CPU温度WMI。没有尝试它,看它是否有效,没有办法告诉。如果没有,请返回主板制造商,看看他们是否提供WMI驱动程序。如果没有,那你就不走运了。
Two problems.

First, the MSAcpi_ThermalZoneTemperature class is in the WMI "root\WMI" namespace. You're using the "root\cimv2" namespace since you're not specifying one in your WMI code.

Second, not all motherboards support reporting CPU temp through WMI. There is no way to tell without just trying it to see if it works. If it doesn't, go back to your motherboard manufacturer and see if they provide WMI drivers. If not, you're out of luck.


这篇关于获取“System.ArgumentOutOfRangeException”在C#中使用WMI时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆