Chris Dewey
Chris Dewey

Reputation: 33

I get an invalid query error when foreach (ManagementObject mo in mos.Get())

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;
        }

    }

}

Hi All im getting invalid query on the line i have marked (##ERROR HERE##) but i cant seem to locate a fix can anyone help. This script should look at the system and get system information.

Photo of the Program

Upvotes: 0

Views: 406

Answers (1)

SmallSoft
SmallSoft

Reputation: 728

Your query is incorrect, you need to insert whitespace between tokens: "SELECT * FROM " + hwclass, then it will work.

Upvotes: 1

Related Questions