在uwp中保存System.Comment或System.Subject文件属性

问题描述 投票:0回答:1

我想将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,说“从调用COM组件返回错误HRESULT E_FAIL”。

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

如何使用GUID标记此文件?

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

c# sqlite uwp guid storagefile
1个回答
1
投票

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.