我知道這個答案已經有一段時間了,但我正在研究一個需要類似功能的項目,我可以告訴你這確實是可能的。據我所知,它確實需要DDK和PInvoke,這個信息沒有C#或WMI接口。它需要打開低級USB根集線器設備並直接向它們發送驅動程序IOCTL命令。
好消息是,微軟提供了一個示例C++應用程序,它可以完全枚舉所有USB設備,並準確顯示它們連接的端口。該應用程序是USBView sample application。
我想你會發現,如果你編譯並運行這個應用程序,你會發現它確實顯示了你的設備插入的位置,如果你插入任何設備到該端口,它顯示在同一個地方。如果你創建一個非託管的C++ DLL,它可以提供一些C#應用程序可以用來獲取所需信息的調用。
它有這樣說的「EnumerateHubPorts()」中的IT部門代碼:
給出一個手柄,一個開放的樞紐和下游端口上 樞紐的數量,送樞紐的IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX 請求集線器的每個下行端口獲取有關連接到每個端口的 設備(如果有)的信息。
想要了解這一切需要的東西(即使您只對一個端口感興趣,所有東西都必須從頂部開始枚舉),下面是在enum.c文件頂部列出的註釋在代碼中:
/*
This source file contains the routines which enumerate the USB bus
and populate the TreeView control.
The enumeration process goes like this:
(1) Enumerate Host Controllers and Root Hubs
EnumerateHostControllers()
EnumerateHostController()
Host controllers currently have symbolic link names of the form HCDx,
where x starts at 0. Use CreateFile() to open each host controller
symbolic link. Create a node in the TreeView to represent each host
controller.
GetRootHubName()
After a host controller has been opened, send the host controller an
IOCTL_USB_GET_ROOT_HUB_NAME request to get the symbolic link name of
the root hub that is part of the host controller.
(2) Enumerate Hubs (Root Hubs and External Hubs)
EnumerateHub()
Given the name of a hub, use CreateFile() to map the hub. Send the
hub an IOCTL_USB_GET_NODE_INFORMATION request to get info about the
hub, such as the number of downstream ports. Create a node in the
TreeView to represent each hub.
(3) Enumerate Downstream Ports
EnumerateHubPorts()
Given an handle to an open hub and the number of downstream ports on
the hub, send the hub an IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX
request for each downstream port of the hub to get info about the
device (if any) attached to each port. If there is a device attached
to a port, send the hub an IOCTL_USB_GET_NODE_CONNECTION_NAME request
to get the symbolic link name of the hub attached to the downstream
port. If there is a hub attached to the downstream port, recurse to
step (2).
GetAllStringDescriptors()
GetConfigDescriptor()
Create a node in the TreeView to represent each hub port
and attached device.
*/
它看起來像我可以通過'HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Enum \ USB'從註冊表獲取位置。一旦我確認了,我會寫一個答案。 –
可以預料到,因爲非WMI API被命名爲['SetupDiGetDeviceRegistryProperty'](http://msdn.microsoft.com/en-us/library/ff551967.aspx) –
看起來像位於HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Enum \ USB在設備插入不同的端口時不會更新 – Derek