winform程序中usb自动识别
如何自动获取到电脑上的usb设备的设备名称和对应的端口号,
private string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{
List<string> strs = new List<string>();
try
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
{
var hardInfos = searcher.Get();
foreach (var hardInfo in hardInfos)
{
if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
{
strs.Add(hardInfo.Properties[propKey].Value.ToString());
}
}
searcher.Dispose();
}
return strs.ToArray();
}
catch
{
return null;
}
finally
{ strs = null; }
}
private void button1_Click(object sender, EventArgs e)
{
string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
for (int i = 0; i < ss.Length; i++)
{
textBox1.Text += ss[i];
}
}
这样的方法可以获得端口号的,但我想获得设备的名称和端口号,从而实现自动配对。有哪位大神给个方法,拜托了!