使用WMI代码创建器创建代码以扫描连接的USB设备

时间:2010-09-15 21:35:08

标签: c# wmi

如何使用WMI Code Creator创建扫描附加USB设备的代码?我已经下载了该实用程序,但我对如何使用它有点迷失。感谢。

1 个答案:

答案 0 :(得分:2)

在WMI Code Creator中选择以下选项:

命名空间:root \ WMI

类:MSWmi_PnPInstanceNames

从结果框中选择InstanceNames以获取以下代码:

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