I would like to associate a GUID with my StorageFile, to make it easier to test whether or not two files are derived from the same source.
I'm trying to save the GUID in a file property:
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);
}
This doesn't work. When it gets to SavePropertiesAsync it throws a COMException saying "Error HRESULT E_FAIL as been returned from a call to a COM component."
The file is an SQLite database in the app directory, with a custom file extension.
How can I tag this file with a GUID?
P.S. After some poking around ... maybe the problem is that I have not registered the file property handlers for the custom file type. Not sure how to do that yet.
The problem is that the custom file type does not support storing of properties.
Properties are stored inside the file and are written and read by Windows 10 using property handlers that you implement.
The following page discusses properties in Windows 10: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776859(v=vs.85).aspx
With respect to using SQLite as a document format, unfortunately, the SQLite file can only be accessed when it is in the app dir. Consequently it is highly inefficient to support file properties because reading or writing the property requires copying the file to the app dir and back.