Windows Property System, introduced in Windows Vista, provides you a uniform mechanism to access Shell items’ metadata. Using WPS you can store and retrieve properties of the items such as folders, files, contacts or emails.

WinRT provides a straightforward method of accessing the most frequently used properties grouped by the file type (Image, Music, Video, etc.) as well as a method of accessing the individual properties.

The following code sample demonstrates how to retrieve image file properties, modify one of them and store it back to the file:

...
var file = await someFolder.GetFileAsync("IMGP8648.JPG");
var fileProperties = await file.Properties.GetImagePropertiesAsync();

fileProperties.Rating = 25;

await fileProperties.SavePropertiesAsync();

Using the same approach you can work with the document, music, video and basic (size, date modified) file properties. An interesting point here that you are able to call GetMusicPropertiesAsync() for a document file and it will return an empty MusicProperties object. But if you will try to change this object and save it back, an exception will be thrown – “Value does not fall within the expected range”. The helper classes do not expose all the available properties. To access the rest of them, you will need to know canonical names for these properties and need to use RetrievePropertiesAsync() function:

var fileProperties = await file.Properties.RetrievePropertiesAsync(
           new List { "System.GPS.Latitude", "System.GPS.Longitude" });

var latitude = fileProperties["System.GPS.Latitude"];

To modify an extended property you need to provide a pair of the property name and the new property value:

var propertyToSave = new List> 
       { new KeyValuePair("System.Photo.LensManufacturer", "Pentax") };

await file.Properties.SavePropertiesAsync(propertyToSave);

Also there is a possibility to get all the properties associated with the file by providing an empty list of the properties to retrieve:

...
var fileProperties = await file.Properties.RetrievePropertiesAsync(new List {});
...

RetrievePropertiesAsync() returns an object of IMap<String, Object> where the first parameter is a property identifier and the second is a value. Below you can see an example of the results returned by this function:

Key: {B725F130-47EF-101A-A5F1-02608C9EEBAC} 14
Value: 13/10/2011 12:02:10 AM -04:00
Key: {6336B95E-C7A7-426D-86FD-7AE3D39C84B4} 100
Value: Auto
Key: {14B81DA1-0135-4D31-96D9-6CBFC9671A99} 272
Value: PENTAX K10D
Key: {14B81DA1-0135-4D31-96D9-6CBFC9671A99} 33434
Value: 0.0005

At first look this is not very informative, right? But if we will check MSDN, we can find that keys consist of the two parts – format ID (GUID) and property ID (number) and all these IDs are described as part of the property schema. For example, the first value - {B725F130-47EF-101A-A5F1-02608C9EEBAC} 14 - represents a date and time of the last write to the file, the last value - {14B81DA1-0135-4D31-96D9-6CBFC9671A99} 33434 – represents the photo expose time.

So this information is not useless, but I hope to see in the future some sort of helpers to convert property IDs into the canonical names such as System.DateModified or System.Photo.ExposureTime.

blog comments powered by Disqus