首页 > 解决方案 > 我可以在 Windows 便携式设备 (WPD) api 中使用部分外壳对象解析名称来获取文件流吗?

问题描述

将文件传输到 Windows 便携式设备 (WPD) api 中的本地存储时,使用 Windows api 代码包 ShellObject 解析名称的最后一个 guid 的第一部分是否始终有效,该名称表示连接 USB 的 android 或 iphone 设备的路径?

例如。从 ShellObject 解析名称的示例获取解析名称调用:

::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_22b8&pid_2e82#zy322kldjb#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\{00000009-0001-0001-0000-000000000000}\{00000009-0001-0001-0000-000000000000}\{00005461-0001-0001-0000-000000000000}

据我所知,这代表:

- My Computer    ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\
- USB device     \\?\usb#vid_22b8&pid_2e82#zy322kldjb#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\
- Root folder    SID-{10001,,26401026048}\
- Folder 1       {00000009-0001-0001-0000-000000000000}\
- Folder 2       {00000075-0001-0001-0000-000000000000}\
- File           {00005461-0001-0001-0000-000000000000}

使用 WPD api枚举Folder 2设备上的内容,我可以看到同一个文件具有类似的文件 id o5461- 这看起来像File上面部分的一部分。

这个假设是否总是正确的 - 我可以像下面的代码一样使用它,将一个文件从 android 手机复制到 c 驱动器上的本地存储吗?还是有时不匹配?另外,文件ID中的“o”是什么意思?

这似乎可行,但我对在生产就绪代码中使用它犹豫不决。

using System.IO;
using PortableDeviceApiLib;

public class Copier
{
  public void PerformCopy()
  {
    var deviceId = @"\\?\usb#vid_22b8&pid_2e82#zy322kldjb#{6ac27878-a6fa-4155-ba85-f98f491d4f33}";
    var sourceItemGuidString = "{00005461-0001-0001-0000-000000000000}";
    var destinationPath = @"C:\Test\";
    var fileName = "Testing.jpg";
    var size = 3738545;

    // get "o5461" from the parsing path from the Shell libraries
    var fileId = "o" + sourceItemGuidString.Replace("{", string.Empty).Replace("}", string.Empty).Split('-')[0].TrimStart('0');

    this.TransferContentFromDevice(deviceId, fileId, size, destinationPath, fileName);
  }

  private void TransferContentFromDevice(string deviceId, string fileId, long length, string saveToPath, string fileName)
  {
    PortableDeviceClass device;

    try
    {
      device = SomehowGetOnePortableDeviceAndConnectToIt(deviceId);

      // Make sure that the target dir exists.
      Directory.CreateDirectory(saveToPath);

      device.Content(out var content);
      content.Transfer(out var resources);

      var property = new _tagpropertykey
      {
        fmtid = new Guid(0xE81E79BE, 0x34F0, 0x41BF, 0xB5, 0x3F, 0xF1, 0xA0, 0x6A, 0xE8, 0x78, 0x42),
        pid = 0
      };

      uint optimalTransferSize = 0;
      resources.GetStream(fileId, ref property, 0, ref optimalTransferSize, out IStream wpdStream);

      var sourceStream = (System.Runtime.InteropServices.ComTypes.IStream)wpdStream;
      using (var targetStream = new FileStream(Path.Combine(saveToPath, fileName), FileMode.Create, FileAccess.Write))
      {
        // Get the total size.
        long written = 0;
        long lPCt = 0;
        unsafe
        {
          var buffer = new byte[1024];
          int bytesRead;
          do
          {
            sourceStream.Read(buffer, 1024, new IntPtr(&bytesRead));
            targetStream.Write(buffer, 0, bytesRead);

            written += 1024;
            long PCt = length > 0 ? (100 * written) / length : 100;
            if (PCt != lPCt)
            {
              lPCt = PCt;
              Console.WriteLine("Progress: " + lPCt);
            }
          } while (bytesRead > 0);
        }
      }          
    }
    finally
    {
      Disconnect(device);
    }
  }
} 

标签: c#winapiusb

解决方案


如果item有一个像下面这样的解析名称,我是在5461最终 guid 的部分之后:

::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_22b8&pid_2e82#zy322kldjb#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,26401026048}\{00000009-0001-0001-0000-000000000000}\{00000075-0001-0001-0000-000000000000}\{00005461-0001-0001-0000-000000000000}

我们可以使用以下代码WPD_OBJECT_IDShellObject. 有关此属性和相关属性的更多信息

string GetFileId(ShellObject item)
{
  const int WPD_OBJECT_ID = 2;      
  var property = item.Properties.GetProperty(new PropertyKey(new Guid("{ef6b490d-5cd8-437a-affc-da8b60ee4a3c}"), WPD_OBJECT_ID));
  return property.ValueAsObject as string;
}

这将返回文件 id o5641,然后可以TransferContentFromDevice直接在我的方法中使用它。

感谢@SimonMourier 为我指明了正确的方向。


推荐阅读