C# 获取已连接USB设备的列表
如何获取windows计算机上所有已连接USB设备的列表?您可能会发现这很有用。下面是一个示例(它P/调用到C# 获取已连接USB设备的列表,c#,usb,C#,Usb,如何获取windows计算机上所有已连接USB设备的列表?您可能会发现这很有用。下面是一个示例(它P/调用到setupapi.dll)。为您的项目添加对System.Management的引用,然后尝试以下操作: namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.Management; // need to add System.Management
setupapi.dll
)。为您的项目添加对System.Management的引用,然后尝试以下操作:
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}
命名空间控制台应用程序1
{
使用制度;
使用System.Collections.Generic;
使用System.Management;//需要将System.Management添加到项目引用中。
班级计划
{
静态void Main(字符串[]参数)
{
var usbDevices=GetUSBDevices();
foreach(usbDevice中的var usbDevice)
{
WriteLine(“设备ID:{0},PNP设备ID:{1},描述:{2}”,
usbDevice.DeviceID、usbDevice.PnpDeviceID、usbDevice.Description);
}
Console.Read();
}
静态列表GetUSBDevices()
{
列表设备=新列表();
管理对象收集;
使用(var searcher=newmanagementobjectsearcher(@“Select*fromWin32_USBHub”))
collection=searcher.Get();
foreach(集合中的var设备)
{
添加(新的USBDeviceInfo)(
(字符串)device.GetPropertyValue(“DeviceID”),
(字符串)device.GetPropertyValue(“PNPDeviceID”),
(字符串)device.GetPropertyValue(“说明”)
));
}
collection.Dispose();
返回装置;
}
}
类USBDeviceInfo
{
公共USBDeviceInfo(字符串deviceID、字符串pnpDeviceID、字符串描述)
{
this.DeviceID=DeviceID;
this.PnpDeviceID=PnpDeviceID;
这个。描述=描述;
}
公共字符串设备ID{get;private set;}
公共字符串PnpDeviceID{get;private set;}
公共字符串说明{get;private set;}
}
}
为了查看我感兴趣的设备,我将Adel Hazzah的代码中的Win32_USBHub替换为基于。这对我很有用:
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program
{
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.Read();
}
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}
命名空间控制台应用程序1
{
使用制度;
使用System.Collections.Generic;
使用System.Management;//需要将System.Management添加到项目引用中。
班级计划
{
静态void Main(字符串[]参数)
{
var usbDevices=GetUSBDevices();
foreach(usbDevice中的var usbDevice)
{
WriteLine(“设备ID:{0},PNP设备ID:{1},描述:{2}”,
usbDevice.DeviceID、usbDevice.PnpDeviceID、usbDevice.Description);
}
Console.Read();
}
静态列表GetUSBDevices()
{
列表设备=新列表();
管理对象收集;
使用(var searcher=newmanagementobjectsearcher(@“Select*fromWin32_PnPEntity”))
collection=searcher.Get();
foreach(集合中的var设备)
{
添加(新的USBDeviceInfo)(
(字符串)device.GetPropertyValue(“DeviceID”),
(字符串)device.GetPropertyValue(“PNPDeviceID”),
(字符串)device.GetPropertyValue(“说明”)
));
}
collection.Dispose();
返回装置;
}
}
类USBDeviceInfo
{
公共USBDeviceInfo(字符串deviceID、字符串pnpDeviceID、字符串描述)
{
this.DeviceID=DeviceID;
this.PnpDeviceID=PnpDeviceID;
这个。描述=描述;
}
公共字符串设备ID{get;private set;}
公共字符串PnpDeviceID{get;private set;}
公共字符串说明{get;private set;}
}
}
如果将ManagementObjectSearcher更改为以下内容:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%""");
所以“GetUSBDevices()看起来像这样”
静态列表GetUSBDevices()
{
列表设备=新列表();
管理对象收集;
使用(var searcher=new ManagementObjectSearcher(@“从Win32中选择*”,其中设备ID类似于“USB%”)
collection=searcher.Get();
foreach(集合中的var设备)
{
添加(新的USBDeviceInfo)(
(字符串)device.GetPropertyValue(“DeviceID”),
(字符串)device.GetPropertyValue(“PNPDeviceID”),
(字符串)device.GetPropertyValue(“说明”)
));
}
collection.Dispose();
返回装置;
}
}
您的结果将仅限于USB设备(与您系统上的所有类型相反)我知道我在回答一个老问题,但我刚刚进行了同样的练习,发现了更多信息,我认为这将对讨论做出很大贡献,并帮助其他发现这个问题并发现现有答案不足之处的人 该值很接近,可以使用该值进行校正。更详细地了解所涉及的WMI类有助于完成这幅图
Win32_USBHub
仅返回USB集线器。事后看来,这似乎很明显,但上面的讨论忽略了这一点。它不包括所有可能的USB设备,只包括那些可以(至少在理论上)作为附加设备集线器的设备。它遗漏了一些不是集线器的设备(特别是复合设备的一部分)
Win32_PnPEntity
确实包括所有USB设备,以及数百个其他非USB设备。建议对以“USB%”开头的设备ID使用WHERE子句搜索Win32_pPentity
来过滤列表,这很有帮助,但有点不完整;它错过了蓝牙设备、一些打印机/打印服务器以及与HID兼容的鼠标和键盘。我看过“USB\%”、“USBSTOR\%”、“USBPRINT\%”、“BTH\%”、“SWD\%”和“HID\%”<但是,代码>Win32_PnPEntity是一个很好的“主”引用,一旦您拥有来自其他来源的PNPDeviceID,就可以查找信息
我发现枚举USB设备的最佳方法是查询Win32\u USBControllerDevice
。虽然它没有给出设备的详细信息,但它完全列举了您的USB设备,并为您提供了一对前置/依赖的PNPDeviceID
s,用于yo上的每个USB设备(包括集线器、非集线器设备和HID兼容设备)
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%"""))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return devices;
}
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine(string.Format("({0}) {1}", drive.Name.Replace("\\",""), drive.VolumeLabel));
}
}
lstResult.Clear();
foreach (ManagementObject drive in new ManagementObjectSearcher("select * from Win32_DiskDrive where InterfaceType='USB'").Get())
{
foreach (ManagementObject partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
{
foreach (ManagementObject disk in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
{
foreach (var item in disk.Properties)
{
object value = disk.GetPropertyValue(item.Name);
}
string valor = disk["Name"].ToString();
lstResult.Add(valor);
}
}
}
}