System.Management.ManagementException: 'Invalid query ' -Please Help

  • Thread starter Thread starter ChrisDewey
  • Start date Start date
C

ChrisDewey

Guest
Hi All

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

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


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

}

}

Continue reading...
 
Back
Top