Pregunta

I've seen atleast two other questions regarding WMI but none had an answer to my question, so here it is;

I was experimenting with the WMI interface in my code. Basically this is what i have right now and it works. But it seems to me i could write it more efficiently:

public bool GetUsbStateById(string id) { bool returnValue = false;

  try
  {
    ObjectQuery query = new ObjectQuery();
    query.QueryString = string.Format("Select * From Win32_PnPDevice");
    ManagementObjectSearcher mySearcher = new ManagementObjectSearcher(query);

    List<ManagementObject> results = (from ManagementObject mo in mySearcher.Get().AsParallel()
                                      where mo["SystemElement"].ToString().ToUpper().Contains(id.ToUpper())
                                      select mo).ToList();

    if (results.Count > 0)
      returnValue = true;
  }
  catch (Exception ex)
  {
    // TODO: implement logging
  }

  return returnValue;
}

So what happens here is that i get a list of ManagementObjects from the ManagementObjectSearcher. This works fine and also returns the exact results as i expect it to work.

But it seems redundant to me. Because, first i get the whole list, and then filter it. But because it uses WQL to fill the list, i assumed that i could implement something like this:

query.QueryString = string.Format("Select * From Win32_PnPDevice where SystemElement Like '%{0}%'",id);

this keeps throwing an exception that the query is not correct.

so i tried this instead:

query.QueryString = string.Format("Select SystemElement From Win32_PnPDevice);

This works as well, so next i tried Win32_PnPDevice.SystemElement, but this didn't work either.

any examples i looked at on the internet showed something like this

Select * From Win32_Service Where Name Like "%SQL%"

but c# can't parse the double quotes that surround the %SQL% statement there, using an the \ escape character yielded no results either.

To test my code and the code posted below i used the WMI Code Creator from Microsoft

¿Fue útil?

Solución

if you want to run like query in WMI then you can use below example:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
              string strSearchText="win";
              string strSearchQuery=string.Format("SELECT * FROM Win32_Service where Name like '%{0}%'",strSearchText);
              ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",strSearchQuery  );

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_Service instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

But you can not apply like query on Win32_PNPDevice as discussed

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top