Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Invalid query" exception without clear reason

Tags:

c#

.net

wmi

What is wrong with next WMi query? (i got "Invalid query" managment exception).

        const string deviceName = "04157DF42C9B1109";

        string wmiQuery = string.Format("SELECT * FROM Win32_USBControllerDevice WHERE Antecedent LIKE '%{0}%'", deviceName);

        ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
        ManagementObjectCollection retObjectCollection = searcher.Get();

        foreach (ManagementObject retObject in retObjectCollection)
        {
            Console.WriteLine("[{0}]:{1}", retObject["Antecedent"], retObject["Dependent"]);
        }
like image 718
Brans Ds Avatar asked Nov 10 '22 07:11

Brans Ds


1 Answers

I couldn't figure out how to fix the original error but the following approach may be used as a workaround:

string wmiQuery = string.Format("SELECT * FROM Win32_USBControllerDevice");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();

var retObjectList = retObjectCollection.Cast<ManagementObject>()
    .Where(m => ((string)m["Antecedent"]).Contains(deviceName))
    .ToList();

foreach (ManagementObject retObject in retObjectList)
{
    Console.WriteLine("[{0}]:{1}", retObject["Antecedent"], retObject["Dependent"]);
}

I noticed other people having the same issue and some people suggested using ASSOCIATORS OF statement (Resource: https://superuser.com/questions/740564/wmi-query-based-on-antecedent-string) That's the accepted answer here so it might just work for you too.

like image 100
Volkan Paksoy Avatar answered Nov 14 '22 23:11

Volkan Paksoy