c# - 我可以在 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);
}
}
}
解决方案
如果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_ID
从ShellObject
. 有关此属性和相关属性的更多信息:
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 为我指明了正确的方向。
推荐阅读
- reactjs - Create-React-App index.html 文件中的元属性未反映在 Google 搜索中
- javascript - 为什么 express api 没有得到这个文件夹?
- python - 从 Python Selenium 更改选择选项的值
- kubernetes - 尝试在 mac 上构建 istio 源时出错
- spring-boot - 使用 React JS 的 Spring Boot 发送邮件
- c# - 检查弹出窗口是否是工具提示 WPF
- typescript - 当键为数字时,Object.values 会去除类型信息
- spring-boot - springboot maven项目中无法启动Redis服务器
- typescript - JS线程被阻塞时React Native Animated.sequence不运行
- python - 无法使用熊猫读取 xlsx 文件