【问题标题】:Saving System.Comment or System.Subject file properties in uwp在 uwp 中保存 System.Comment 或 System.Subject 文件属性
【发布时间】:2018-05-30 06:17:05
【问题描述】:

我想将 GUID 与我的 StorageFile 相关联,以便更轻松地测试两个文件是否来自同一源。

我正在尝试将 GUID 保存在文件属性中:

        private static string GuidProperty = "System.Comment"; // also tried "System.Subject". 

        static public async Task<Guid> GetGuidAsync( StorageFile file)
        {
            var properties = await file.Properties.RetrievePropertiesAsync(new string[] { GuidProperty });
            if (!(properties[GuidProperty] is string guidString)) {
                throw new InvalidOperationException("Missing GUID on file.");
            }
            return new Guid(guidString);
        }

        static public async Task InitializeGuidAsync( StorageFile file)
        {
            var properties = await file.Properties.RetrievePropertiesAsync(new string[] { GuidProperty });
            properties[GuidProperty] = Guid.NewGuid().ToString();
            await file.Properties.SavePropertiesAsync(properties);
        }

这不起作用。当它到达 SavePropertiesAsync 时,它会抛出一个 COMException,说“错误 HRESULT E_FAIL 已从对 COM 组件的调用中返回”。

该文件是应用目录中的 SQLite 数据库,具有自定义文件扩展名。

如何使用 GUID 标记此文件?

附:经过一番摸索......也许问题是我没有为自定义文件类型注册文件属性处理程序。还不知道该怎么做。

【问题讨论】:

  • 我尝试使用FileOpenPicker 获取文件,然后调用SavePropertiesAsync,它在我这边有效,您能否分享有关您要添加属性的文件的更多详细信息?
  • 在下面查看我的回复(这里太长了,不适合)
  • 我在上面编辑了我的帖子以包含有关文件类型的信息。

标签: c# sqlite uwp guid storagefile


【解决方案1】:

问题是自定义文件类型不支持存储属性。

属性存储在文件中,并由 Windows 10 使用您实现的属性处理程序进行写入和读取。

以下页面讨论了 Windows 10 中的属性:https://msdn.microsoft.com/en-us/library/windows/desktop/bb776859(v=vs.85).aspx

关于使用 SQLite 作为文档格式,不幸的是,SQLite 文件只有在 app 目录下才能访问。因此,支持文件属性的效率非常低,因为读取或写入属性需要将文件复制到应用程序目录并返回。

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 2015-06-19
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多