首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当foreach (mos.Get()中的ManagementObject mo )时,我得到一个无效的查询错误

当foreach (mos.Get()中的ManagementObject mo )时,我得到一个无效的查询错误
EN

Stack Overflow用户
提问于 2018-08-16 04:55:57
回答 1查看 149关注 0票数 0
代码语言:javascript
复制
using System;
using System.Collections.Generic;
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())    ##ERROR HERE##

            {
                details.Add(mo[syntax].ToString());
            }

            return details;
        }

    }

}

嗨,我在我已经标记的线路(##ERROR,HERE##)上得到了无效的查询,但我似乎找不到修复的方法,有人能帮助我吗?此脚本应查看系统并获取系统信息。

Photo of the Program

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-16 12:26:28

您的查询是错误的,您需要在标记之间插入空格:"SELECT * FROM " + hwclass,然后它将正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51866306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档