tags:

views:

10

answers:

1

How can I use the WMI Code Creator to create code to scan for attached USB devices? I've downloaded the utility, but I'm a bit lost on how to use it. Thanks.

A: 

In the WMI Code Creator select these options:

Namespace: root\WMI

Class: MSWmi_PnPInstanceNames

Select InstanceNames from the Results box for the following code:

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

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\WMI", 
                    "SELECT * FROM MSWmi_PnPInstanceNames"); 

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