연결된 USB 장치 목록 가져 오기
Windows 컴퓨터에 연결된 모든 USB 장치 목록을 얻으려면 어떻게해야합니까?
프로젝트의 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; }
}
}
나는 이전 질문에 답하고 있다는 것을 알고 있지만, 동일한 연습을 통해 조금 더 많은 정보를 찾았습니다.이 정보는 토론에 많은 기여를 할 것이며이 질문을 찾은 다른 사람을 도울 것입니다. 기존 답변이 부족합니다.
허용 대답은 가까이, 그리고 사용하여 수정 될 수 Nedko의 의견 그것입니다. 관련된 WMI 클래스에 대한 자세한 이해는 그림을 완성하는 데 도움이됩니다.
Win32_USBHub
USB 허브 만 반환합니다 . 돌이켜 보면 명백해 보이지만 위의 논의는 그것을 놓친다. 여기에는 가능한 모든 USB 장치가 포함되지 않고 추가 장치의 허브 역할을 할 수있는 USB 장치 만 포함됩니다. 허브가 아닌 일부 장치 (특히 복합 장치의 일부)가 누락됩니다.
Win32_PnPEntity
모든 USB 장치와 수백 개의 비 USB 장치가 포함됩니다. 목록을 필터링하기 위해 "USB %"로 시작하는 DeviceID에 대한 WHERE 절 검색을 사용 하라는 Russel Gantman의 조언 Win32_PnPEntity
은 유용하지만 약간 불완전합니다. 블루투스 장치, 일부 프린터 / 인쇄 서버, HID 호환 마우스 및 키보드가 누락되었습니다. "USB \ %", "USBSTOR \ %", "USBPRINT \ %", "BTH \ %", "SWD \ %"및 "HID \ %"를 보았습니다. Win32_PnPEntity
그러나 다른 소스에서 PNPDeviceID를 소유하게되면 정보를 조회하기위한 좋은 "마스터"참조입니다.
내가 찾은 것은 USB 장치를 열거하는 가장 좋은 방법은 쿼리하는 것 Win32_USBControllerDevice
입니다. 장치에 대한 자세한 정보를 제공하지는 않지만 USB 장치를 완전히 열거 PNPDeviceID
하고 모든 USB 장치 (허브, 비 허브 장치 및 HID 호환 장치 포함)에 대한 선행 / 종속 쌍 을 제공합니다. 체계. 쿼리에서 반환 된 각 종속 항목은 USB 장치입니다. Antecedent는 쿼리에 의해 반환 된 USB 컨트롤러 중 하나 인 할당 된 컨트롤러 Win32_USBController
입니다.
보너스로 WMI 는 쿼리에 응답 할 때 장치 트리Win32_USBControllerDevice
를 탐색하므로 이러한 결과가 반환되는 순서는 부모 / 자식 관계를 식별하는 데 도움이 될 수 있습니다. (이것은 문서화되지 않았으므로 추측 일뿐 입니다. 확실한 결과를 얻으 려면 SetupDi API의 CM_Get_Parent (또는 Child + Sibling )를 사용하십시오.) SetupDi API에 대한 옵션으로 아래에 나열된 모든 장치를 Win32_USBHub
조회 할 수있는 것으로 보입니다. 레지스트리 (at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\ + PNPDeviceID
) ParentIdPrefix
에 있고 하위 PNPDeviceID에있는 마지막 필드의 접두사가 될 매개 변수 가 있으므로 Win32_PnPEntity
쿼리 를 필터링하기 위해 와일드 카드 일치에도 사용할 수 있습니다 .
내 응용 프로그램에서 다음을 수행했습니다.
- (선택 사항)
Win32_PnPEntity
나중에 검색 할 수 있도록 결과를 쿼리 하여 키-값 맵 (키로 PNPDeviceID 사용)에 저장했습니다. 나중에 개별 쿼리를 수행하려는 경우 선택 사항입니다. Win32_USBControllerDevice
내 시스템 (모든 종속 항목)에있는 USB 장치의 최종 목록을 쿼리 하고 이들의 PNPDeviceID를 추출했습니다. 장치 트리를 따르는 순서에 따라 장치를 루트 허브 (컨트롤러가 아닌 반환 된 첫 번째 장치)에 할당하고 parentIdPrefix를 기반으로 트리를 구축했습니다. SetupDi를 통해 장치 트리 열거와 일치하는 쿼리가 반환하는 순서는 각 루트 허브 (전위가 컨트롤러를 식별 함)와 그 아래에있는 장치 (예 : 내 시스템)의 반복입니다.- 첫 번째 컨트롤러의 루트 허브
- 두 번째 컨트롤러의 루트 허브
- 두 번째 컨트롤러의 루트 허브 아래 첫 번째 허브 (parentIdPrefix 포함)
- 두 번째 컨트롤러의 루트 허브 아래에있는 첫 번째 허브 아래의 첫 번째 복합 장치 (PNPDeviceID는 허브의 ParentIdPrefix 위에 일치하며 자체 ParentIdPrefix를 가짐)
- 복합 장치의 HID 장치 부분 (PNPDeviceID가 복합 장치의 ParentIDPrefix 위에 일치 함)
- 두 번째 컨트롤러의 루트 허브 아래 첫 번째 허브 아래 두 번째 장치
- 복합 장치의 HID 장치 부분
- 두 번째 컨트롤러의 루트 허브 아래에있는 첫 번째 허브 아래의 첫 번째 복합 장치 (PNPDeviceID는 허브의 ParentIdPrefix 위에 일치하며 자체 ParentIdPrefix를 가짐)
- 두 번째 컨트롤러의 루트 허브 아래에있는 두 번째 허브
- 두 번째 컨트롤러의 루트 허브 아래 두 번째 허브 아래 첫 번째 장치
- 두 번째 컨트롤러의 루트 허브 아래에있는 세 번째 허브
- 기타
- 두 번째 컨트롤러의 루트 허브 아래 첫 번째 허브 (parentIdPrefix 포함)
- 쿼리
Win32_USBController
. 이것은 장치 트리의 맨 위에있는 컨트롤러의 PNPDeviceID에 대한 자세한 정보를 제공했습니다 (이전 쿼리의 선행 항목). 이전 단계에서 파생 된 트리를 사용하여 하위 (루트 허브)와 하위 (다른 허브) 및 하위 (허브가 아닌 장치 및 복합 장치) 및 하위 등을 반복적으로 반복했습니다.- 첫 번째 단계에서 저장된 맵을 참조하여 내 트리의 각 장치에 대한 세부 정보를 검색했습니다. (선택적으로 첫 번째 단계를 건너
Win32_PnPEntity
뛰고이 단계에서 정보를 얻기 위해 PNPDeviceId를 사용하여 개별적으로 쿼리 할 수 있습니다. 어느 순서가 더 나은지 결정하는 CPU 대 메모리 절충안 일 것입니다.)
- 첫 번째 단계에서 저장된 맵을 참조하여 내 트리의 각 장치에 대한 세부 정보를 검색했습니다. (선택적으로 첫 번째 단계를 건너
요약하면 Win32USBControllerDevice
Dependents는 시스템에있는 USB 장치의 전체 목록이며 (컨트롤러 자체는 해당 쿼리의 전조 인 컨트롤러 제외) PNPDeviceId
레지스트리 및 언급 된 다른 쿼리의 정보와 이러한 쌍을 상호 참조하여 , 상세한 그림을 구성 할 수 있습니다.
관심이있는 장치를보기 위해이 게시물을 기반으로 Adel Hazzah의 코드에서로 교체 Win32_USBHub
했습니다 . 이것은 나를 위해 작동합니다.Win32_PnPEntity
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; }
}
}
이것은 이동식 USB 드라이브 만 찾는 사람들에게 훨씬 더 간단한 예입니다.
using System.IO;
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
Console.WriteLine(string.Format("({0}) {1}", drive.Name.Replace("\\",""), drive.VolumeLabel));
}
}
Adel Hazzah's answer gives working code, Daniel Widdis's and Nedko's comments mention that you need to query Win32_USBControllerDevice and use its Dependent property, and Daniel's answer gives a lot of detail without code.
Here's a synthesis of the above discussion to provide working code that lists the directly accessible PNP device properties of all connected USB devices:
using System;
using System.Collections.Generic;
using System.Management; // reference required
namespace cSharpUtilities
{
class UsbBrowser
{
public static void PrintUsbDevices()
{
IList<ManagementBaseObject> usbDevices = GetUsbDevices();
foreach (ManagementBaseObject usbDevice in usbDevices)
{
Console.WriteLine("----- DEVICE -----");
foreach (var property in usbDevice.Properties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
}
Console.WriteLine("------------------");
}
}
public static IList<ManagementBaseObject> GetUsbDevices()
{
IList<string> usbDeviceAddresses = LookUpUsbDeviceAddresses();
List<ManagementBaseObject> usbDevices = new List<ManagementBaseObject>();
foreach (string usbDeviceAddress in usbDeviceAddresses)
{
// query MI for the PNP device info
// address must be escaped to be used in the query; luckily, the form we extracted previously is already escaped
ManagementObjectCollection curMoc = QueryMi("Select * from Win32_PnPEntity where PNPDeviceID = " + usbDeviceAddress);
foreach (ManagementBaseObject device in curMoc)
{
usbDevices.Add(device);
}
}
return usbDevices;
}
public static IList<string> LookUpUsbDeviceAddresses()
{
// this query gets the addressing information for connected USB devices
ManagementObjectCollection usbDeviceAddressInfo = QueryMi(@"Select * from Win32_USBControllerDevice");
List<string> usbDeviceAddresses = new List<string>();
foreach(var device in usbDeviceAddressInfo)
{
string curPnpAddress = (string)device.GetPropertyValue("Dependent");
// split out the address portion of the data; note that this includes escaped backslashes and quotes
curPnpAddress = curPnpAddress.Split(new String[] { "DeviceID=" }, 2, StringSplitOptions.None)[1];
usbDeviceAddresses.Add(curPnpAddress);
}
return usbDeviceAddresses;
}
// run a query against Windows Management Infrastructure (MI) and return the resulting collection
public static ManagementObjectCollection QueryMi(string query)
{
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(query);
ManagementObjectCollection result = managementObjectSearcher.Get();
managementObjectSearcher.Dispose();
return result;
}
}
}
You'll need to add exception handling if you want it. Consult Daniel's answer if you want to figure out the device tree and such.
If you change the ManagementObjectSearcher to the following:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
@"SELECT * FROM Win32_PnPEntity where DeviceID Like ""USB%""");
So the "GetUSBDevices() looks like this"
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;
}
}
Your results will be limited to USB devices (as opposed to all types on your system)
You may find this thread useful. And here's a google code project exemplifying this (it P/Invokes into setupapi.dll
).
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);
}
}
}
}
참고URL : https://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices
'code' 카테고리의 다른 글
.js 파일에 .js 파일 포함 (0) | 2020.09.17 |
---|---|
git의 혼란스러운 오류 메시지 (0) | 2020.09.16 |
Xcode 4-내가 삭제 한 키 체인 인증서를 다시 설치합니다. (0) | 2020.09.16 |
Apache가 심볼릭 링크를 따르지 않음 (403 금지됨) (0) | 2020.09.16 |
zsh에서 찾을 수없는 명령 (0) | 2020.09.16 |