Windows 8 and now also Windows Phone 8.1 enable apps access to the filesystem. This API is very limited unfortunately (primarily for security reasons), so by setting several Capabilities in app’s manifest you can access directly just a few basic pseudo-folders – libraries Pictures, Music and Videos (and for enterprise apps in Windows 8 also Documents), but access to other folders directly is only possible through pickers. These return a non-persistent file/folder object, that can’t be accessed after the app closes or terminates. StorageApplicationPermissions class comes to the rescue.
How is it useful?
StorageApplicationPermissions class contains two useful properties: FutureAccessList and MostRecentlyUsedList . The first of them enables you to “save” a file or folder, you have already gotten access to (e.g. by asking user for permission via FileOpenPicker or FolderPicker) and generate a unique key, that will be able to retrieve the item again anytime in the future. This access is really time unlimited and can hold up to 1 000 different items at once, if you can accept the little burden of having to care for the list by deleting no longer needed items. MostRecentlyUsedList works exactly on the same principle, but is limited to just 25 items, which it cares for automatically (even holds link when files are moved across the disk). After the 25 item limit is reached, oldest item is automatically deleted.
How to use it?
It is very simple! We will show an example of FutureAccessList , for the alternative list the usage is exactly the same as both implement the IStorageItemAccessList .
To add a new item on the list call the Add method:
string token = StorageApplicationPermissions.FutureAccessList.Add(file, metadata);
First argument is the saved folder or file and the second (optional) is a string with additional info for your use.
Important step, you need to perform now is to save the returned string token to a persistent storage like a file or a database.
This is essential, because the string token the only way we can access the item again, using the GetItemAsync method:
IStorageItem item = await StorageApplicationPermissions.FutureAccessList.GetItemAsync( token );
The returned object is of IStorageItem type, that can be casted to StorageFolder or StorageFile as needed. To skip this step if you know the type of your item you can use the GetFileAsync or GetFolderAsync , which return precisely these concrete types. And as you can also see from the name of all these methods – they use the async-await pattern.
Important note: The same way as most WinRT methods, that access files and folders, you will want to wrap the invocation of the GetXXX methods with a try-catch block, because in case the list does not contain the token you supplied, a runtime exception is thrown.