我正在尝试使用 Windows Portable Devices API 和此 API 提供的 PortableDeviceManager 枚举 Windows 上连接的便携式设备。
我已经按照 MSDN 文档链接和各种博客链接实现了设备 ID 的枚举,但是它们都会导致相同的问题 -当有多个连接时,我只能得到它来给我一个设备的 ID。
这是我正在使用的 C# 代码片段:
PortableDeviceManagerClass deviceManager = new PortableDeviceManagerClass();
deviceManager.RefreshDeviceList();
uint numberOfDevices = 1;
deviceManager.GetDevices(null, ref numberOfDevices);
if (numberOfDevices == 0)
{
return new string[0];
}
string [] deviceIds = new string[numberOfDevices];
deviceManager.GetDevices(ref deviceIds[0], ref numberOfDevices);
return deviceIds;
我有两台设备连接到我的电脑,一台可移动 USB 记忆棒和一台数码相机。当两者都处于活动状态时,只会返回我的相机的设备 ID。当我停用相机时,会返回可移动 U 盘的设备 ID。
有没有人有这个 API 的经验,可以指出我做错的方向?