如何使用WMI Code Creator创建扫描附加USB设备的代码?我已经下载了该实用程序,但我对如何使用它有点迷失。感谢。
答案 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);
}
}
}
}