The other day I was researching the different file access scenarios available for Windows 8. I needed the research for my chapter on accessing data in my Windows 8 Book (more on that later). The restrictive sandbox that Windows 8 applications operate in can prove challenging. To access data outside the sandbox and Windows libraries you have to use the pickers. There is a FileSavePicker and the FileOpenPicker. These pickers broker the interaction of your Windows 8 application and the operating system. Your application gets StorageFile or a StorageFolder object back from the the picker.
If you are writing a text editing application if could get annoying have to have the user interact with this picker every time they want to work with the same file. Windows provides a way for user to “give permission” to files the choose through the picker that persists across sessions. In this post I will create a simple text editor that allows the user to save their work and have that work loaded the next time they launch the application.
StorageApplicationPermissions
The StorageApplicationPermissions provides access to lists that allows you application to track recently accessed files. I will show you how to add files to one of these lists and how to access the file in a different session. There are two list available the FutureAccessList and the MostRecentlyUsedList. These list don’t actually store the files, they store a reference to where the file is located.
I started out by creating a simple text editing application that contains a multi-line textbox and two app bar buttons. The first app bar button is a ‘Save As’ button. Here is the code for the save as button:
The first thing I do is get the StorageFile object back from the save picker. If a file is returned I clear out the FutureAccessList. I only want one file in my list but you can store many files in the list. I then add the file to the list. The add method returns a token that you use to retrieve the file. You may want to persist this token for later use. I am not saving my token instead I will retrieve my token at runtime.
Here is the code to the save:
The first thing I do is check to see if there is a file in the FutureAccessList. If there is that is the file I want to use save my work. You can see that I am getting the token for this file and passing it to the GetFileAsync method. If there is no file in the list I use the save picker to get a file.
The Dawn of a new Session
To see the real power of these list you have to launch a new session of your application. These lists persist across sessions so close you application and re-launch it. For my example I retrieve the file in the OnNavigatedTo method of my page.
As you can see the code for retrieving the file is the same as it was in the save method. That is it. My application accessed a file that wasn’t in the sandbox or one of the special library folders, without have to display the FileOpenPicker.
Permission to the file was inferred when they created the file in the picker. This permission is per user and for this application only. One of the interesting things about these lists is that they track the files location even when you application isn’t running. So if the file is moved to another location and the user has permission to that location this example will still work!
Summary
In this post I took a look at the StorageApplicationPermissions object. We saw how we can use this object to make file access easier, providing a better experience for our users. This was a simple example but one that shows the possibilities.
You can download the bit here.