Iris Classon
Iris Classon - In Love with Code

WinRT- grabbing the last file the user accessed FutureAccessList and MostRecentlyUsedList

I have to apologize the shortness and simple posts I’ve written the past few weeks- I realized today that is has been a while since I’ve done a longer post. Currently working on a course for Pluralsight, and being somewhat overwhelmed over the learning curve at work has left me with little time. I am howver working on some RabbitMQ posts (I’m learning messaging as it is something our system at work relies heavily on) and I should have a post cleaned up and ready to go later this week.

Here is however a post for us WinRT devs. A short one, but a useful one. Working on a side project with a mediaplayer I had the peculiar problem of trying to access a file a user had picker earlier in my app without having to copy it locally. WinRT is sandboxed, so simply grabbing it from the path wasn’t an option for me. What we do have is something called a FutureAccessList.

To clarify. The user has to select the file or for using a picker OR you need todeclare that you are accessing one of the KnownFolders (documents, pictures, video), look for the file, grab it and then add it to the list. The sandboxing is standard with WinRT. You cannot know which file the user last accessed generally, not even with PowerShell would that be easy to do. If its a modified file you could recurse (in PowerShell) over all folders and use the lastwritetime first on folders, then files after grabbing last folder updated. But that’s PowerShell. This is WinRT. technically you could create a brokered component that would let you do that, but you would be limited to side-loading the application, and not being able to use it in the Windows Phone app.

This is a list that that stores files (or rather, a token for a file or folder) for future access. There is also the:

StorageApplicationPermissions.MostRecentlyUsedList

Which you can save the most recently used file to.

Say that you let the user use a FilePicker to grab a file (be aware that the way the picker works differs between Windows Phone and Windows Store).

You would probably do this:

var file = await picker.PickSingleFileAsync();

Once you have the file you can add it to the FutureAccessList like this:

Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file)

If you save the token somewhere, for example as a setting:

public void SaveToken(string token)

{

var settings = ApplicationData.Current.LocalSettings;

settings .Values[“token”] = token;

}

You can then later retrieve the token:

public string GetToken()

{

var settings = ApplicationData.Current.LocalSettings;

if (settings .Values.ContainsKey(“token”))

return settings .Values[“token”].ToString();

return “”;

}

Once you have the token again you can access the file by using the token:

var file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);

Quite neat, and simple. By using an access cache we can work with files outside the sandbox without having to break the box. The list is persistent across sessions and updates, but it doesn’t mean that the file is. You can check it a file already consists in a list with:

FutureAccessList.ContainsItem(token);

And:

FutureAccessList.MaximumItemsAllowed;

Let’s us know how many items the list can hold.

Comments

Leave a comment below, or by email.


Last modified on 2014-09-17

comments powered by Disqus