我正在尝试编写一个应用程序,将数据从 Windows 可移植设备(此处为 Samsung Galaxy Note 2)上的特定文件夹传输到我的 PC。
我能够成功列出所有文件夹和文件,并且还可以通过 WPD-API 访问我想要的特定文件夹。
在this page的帮助下我编写了一个名为“PortableDevice”的类,它包装了设备上的每个对象,并从中创建了一个 PortableDeviceObject(这是一个自制的类,不属于 API) 这是包装方法的示例。
private static PortableDeviceObject WrapObject(IPortableDeviceProperties properties, string objectId)
{
PortableDeviceApiLib.IPortableDeviceKeyCollection keys;
properties.GetSupportedProperties(objectId, out keys);
PortableDeviceApiLib.IPortableDeviceValues values;
properties.GetValues(objectId, keys, out values);
// Get the name of the object
string name;
var property = new PortableDeviceApiLib._tagpropertykey();
property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
property.pid = 4;
values.GetStringValue(property, out name);
// Get the type of the object
Guid contentType;
property = new PortableDeviceApiLib._tagpropertykey();
property.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
property.pid = 7;
values.GetGuidValue(property, out contentType);
var folderType = new Guid(0x27E2E392, 0xA111, 0x48E0, 0xAB, 0x0C, 0xE1, 0x77, 0x05, 0xA0, 0x5F, 0x85);
var functionalType = new Guid(0x99ED0160, 0x17FF, 0x4C44, 0x9D, 0x98, 0x1D, 0x7A, 0x6F, 0x94, 0x19, 0x21);
if (contentType == folderType || contentType == functionalType)
{
return new PortableDeviceFolder(objectId, name);
}
return new PortableDeviceFile(objectId, name);
}
当我访问 name 属性时,我以为我总是获得当前对象的全名(图片或音乐文件等)。但仅显示带有全名的图片(例如 test_picture.jpeg),但当我阅读文件夹中的 PDF 时,它仅打印出 PDF 的名称,而不打印出 .PDF 扩展名。 这是有问题的,因为我想迭代文件夹中的所有文件,想要获取数据流并将该流填充到我的电脑上的新文件中。到目前为止它可以工作,但是所有文件(图片除外)都会在没有正确扩展名的情况下创建,因此无法使用或必须手动重命名。
所以问题是:如何访问 WPD 上文件夹中每个文件的完整路径?
最佳答案
我在this site的评论中找到了答案:
我只需更换
return new PortableDeviceFile(objectId, name);
与
property.pid = 12;//WPD_OBJECT_ORIGINAL_FILE_NAME
values.GetStringValue(property, out name);
return new PortableDeviceFile(objectId, name);
关于c# - 获取 Windows 可移植设备上文件的全名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18059234/