对我来说,问题解决了。首先,让我解释一下效果:在我的 Windows 10 设备管理器的便携式设备列表中,有 4 个设备来自读卡器和插入位置 2 的三星平板电脑。Interop.PortableDeviceApiLib.dll 有问题要检索更多的那个此处描述的设备:在 C# 中枚举 Windows 便携式设备
对于此问题,GetDevices() 仅返回一个设备,在我的情况下,是读卡器中没有文件的最后一个设备。当再连接一个设备(另一部智能手机)时,它会成为最后一个设备,并且代码(Windows Portable Devices.zip 来自Accessing an MTP device in Visual Basic .NET)工作正常。
解决方案:我按照已接受的答案(在 C# 中枚举 Windows 便携式设备)中的说明修复了互操作程序集中的编组问题,并将新的互操作 dll 复制到所有现有的 Interop.PortableDeviceApiLib.dll 上(在参考文献中将嵌入互操作类型设置为 False) . PortableDeviceCollection.cs 中的代码(此处的 zip 包:Accessing an MTP device in Visual Basic .NET)我已更改如下:
public void Refresh()
{
this._deviceManager.RefreshDeviceList();
// Determine how many WPD devices are connected
// new, according to https://docs.microsoft.com/en-us/windows/win32/wpd_sdk/enumerating-devices
uint count = 1;
string[] s = null;
this._deviceManager.GetDevices(s, ref count);
// Retrieve the device id for each connected device
var deviceIds = new string[count];
// old: this._deviceManager.GetDevices(ref deviceIds[0], ref count);
this._deviceManager.GetDevices(deviceIds, ref count);
foreach(var deviceId in deviceIds)
{
Add(new PortableDevice(deviceId));
}
}
不幸的是,Interop.PortableDeviceApiLib.dlls 必须在 Visual Studio 中每次 Clean 后替换为新创建的 dll。编辑:在 Andrew Trevarrow 的文章中,Fun with MTP in C# ( http://andrewt.com/2013/06/15/fun-with-mtp-in-c.html ) 是防止此问题的提示:完成后,从 C# 项目“PortableDevices”中删除对 PortableDeviceApiLib 类型库的引用,并添加对新 Interop.PortableDeviceApiLib.dll 的引用(在单独的文件夹中)并将 dll 的“嵌入互操作类型”更改为 False。