【问题标题】:WPD/MTP PortableDevices, Content is always empty (Windows, VisualStudio 2012, VB.NET)WPD/MTP 便携式设备,内容始终为空(Windows、Visual Studio 2012、VB.NET)
【发布时间】:2019-12-28 20:37:44
【问题描述】:

问题已使用 NuGet 包 PortableDevices 进行测试,解决方案来自此处:Accessing an MTP device in Visual Basic .NET

我想从三星 Android 平板电脑获取文件列表。我可以连接,但 GetContents() 不返回任何文件(root.Files.Count = 0)。提前致谢。

Dim Devices As New PortableDeviceCollection()
Devices.Refresh()
Dim tablet As PortableDevice
tablet = Devices.First()
tablet.Connect()
Dim root = tablet.GetContents()

【问题讨论】:

    标签: android vb.net wpd


    【解决方案1】:

    对我来说,问题已经解决了。首先,让我解释一下效果:在我的 Windows 10 设备管理器的便携式设备列表中,有 4 个设备来自读卡器和插入位置 2 的三星平板电脑。Interop.PortableDeviceApiLib.dll 有问题要检索更多的那个此处描述的设备:Enumerating Windows Portable Devices in C# 对于这个问题,GetDevices() 只返回一个设备,在我的情况下,是读卡器中最后一个没有文件的设备。当再连接一台设备(另一部智能手机)时,它会成为最后一台设备,并且代码(来自Accessing an MTP device in Visual Basic .NET 的 Windows Portable Devices.zip)工作正常。

    解决方案:我按照已接受的答案 (Enumerating Windows Portable Devices in C#) 中的说明修复了互操作程序集中的编组问题,并将新的互操作 dll 复制到所有现有的 Interop.PortableDeviceApiLib.dll 上(在 Refs 中将 Embed Interop Types 设置为 False) . PortableDeviceCollection.cs 中的代码(来自此处的压缩包: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.dll 必须在 Visual Studio 中的每次清理后替换为新创建的 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 2013-09-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 2011-09-03
      • 2013-05-19
      相关资源
      最近更新 更多