Reputation: 33
today i wrote my second Code in C# why wont it work? the code ist (as it seems) correct!
error code: InvalidQuery
Code:
static void Main(string[] args)
{
GetComponent("Win32_Processor", "Name");
Console.Read();
Console.ReadKey();
}
private static void GetComponent(string hwclass, string syntax)
{
ManagementObjectSearcher mos = new ManagementObjectSearcher ("root\\CIMV2","SELECT * FROM" + hwclass);
foreach(ManagementObject mj in mos.Get())
{
Console.WriteLine(Convert.ToString(mj[syntax]));
}
}
Upvotes: 1
Views: 172
Reputation: 186688
Please, use formatting or string interpolation (C# 6.0+) to avoid syntax errors:
private static void GetComponent(string hwclass, string syntax) {
//DONE: keep query readable
string query =
$@"select *
from {hwclass}"; // <- you've missed space here
//DONE: wrap IDisposable into using
using (ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", query)) {
foreach(ManagementObject mj in mos.Get())
Console.WriteLine(Convert.ToString(mj[syntax]));
}
}
Upvotes: 4
Reputation: 7783
You are missing a space after the "FROM":
("root\\CIMV2","SELECT * FROM" + hwclass);
Change to:
("root\\CIMV2","SELECT * FROM " + hwclass);
Upvotes: 3