System.Management.ManagementException:'无效查询' - 请帮助 [英] System.Management.ManagementException: 'Invalid query ' -Please Help

查看:774
本文介绍了System.Management.ManagementException:'无效查询' - 请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我是新来的,使用下面的脚本会出现错误  System.Management.ManagementException:'无效查询'

I new to this and using the script below comes up with an error System.Management.ManagementException: 'Invalid query '

它说这个错误在foreach中(管理对象mo在mos.Get()) 

it says that this error is in foreach (ManagementObject mo in mos.Get()) 

但我不能使用System查看修复

but i cant see the fix

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;

namespace NetPrimate_Provisioning_Tool_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGet_Click(object sender, EventArgs e)
        {

            foreach (string cpu in GetComponents("WIN32_Processor", "Name"))
            {
                txtInfo.AppendText("CPU:" + cpu + Environment.NewLine);
            }

            foreach (string gpu in GetComponents("WIN32_VideoController", "Name"))
            {
                txtInfo.AppendText("GPU:" + gpu + Environment.NewLine);
            }

            foreach (string os in GetComponents("WIN32_OperatingSystem", "Caption"))
            {
                txtInfo.AppendText("OS:" + os);
                if(Environment.Is64BitOperatingSystem)
                {
                    txtInfo.AppendText("64Bit" + Environment.NewLine);
                }
                else
                {
                    txtInfo.AppendText("32Bit" + Environment.NewLine);
                }
            }

            string ram = GetComponents("WIN32_ComputerSystem", "TotalPhysicalMemory")[0];

            double db_ram = Convert.ToDouble(ram) / 1073741824;

            int size = (int)Math.Ceiling(db_ram);

            txtInfo.AppendText("RAM:" + size.ToString() + "GB");

        }

        public List<string> GetComponents(string hwclass, string syntax)
        {
            List<string> details = new List<string>();

            ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT*FROM " + hwclass);

            foreach(ManagementObject mo in mos.Get())
                
            {
                details.Add(mo[syntax].ToString());
            }



            return details;
        }
     
    }

}

推荐答案

您好ChrisDewey,

Hi ChrisDewey,

感谢您在此发帖。

对于你的问题,我测试你的代码。它是由查询字符串引起的。请更改
SELECT
* FROM
来自以下代码。

For your question, I test your code. It caused by the query string. Please change SELECT * FROM from the following code.

更改

 ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT*FROM " + hwclass);

 ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM " + hwclass);


效果很好。

It works well.

最诚挚的问候,

Wendy


这篇关于System.Management.ManagementException:'无效查询' - 请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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