Store the access to files/folders instead of using a picker each time !

, , 1 Comment

In a Metro Win8’s style app you have full access to the local, roaming and temp folder of the app. The app can also access the libraries(documents, videos, pictures, …) if the correct capabilities are declared in the package manifest.

If the app needs to retrieve any other file or folder, it has to ask the user through a picker. This is a one-time UI which gives you access to the desired item only if the user is OK with that.

It can be boring (and it is) for the user to give you access to a file each time you need it. In this post we will discuss the solution proposed by the WinRT Framework.

This is in fact as easy as managing a list of strings !

Everything takes place in the FutureAccessList object of the Windows.Storage.AccessCache.StorageApplicationPermissions namespace. This object is a dictionnary of the the files/folder you want to keep an access to. The key of the dictionnary is a string token and the value is the storage item (file / folder).
[csharp]
var picker = new FolderPicker();
picker.FileTypeFilter.Add("*");
var folder = await picker.PickSingleFolderAsync();

StorageApplicationPermissions.FutureAccessList.AddOrReplace(Token, folder);
[/csharp]
The app still have to ask once for the folder but the next time, it can retrieve the folder from this list instead of using a filer/folder picker. The app will then have access to it even if the app is terminated between the two use of it.
[csharp]
var folder = await StorageApplicationPermissions
.FutureAccessList.GetFolderAsync(Token);

var fileToCopy = await StorageFile
.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Logo.png"));
await fileToCopy.CopyAsync(folder, "Logo.png", NameCollisionOption.ReplaceExisting);

[/csharp]

The list is cleared if the app is uninstalled or if you call the clear method on it. The MaximumItemsAllowed property of the FutureAccessList give you … the maximum number of item you can store in the list. At this time it seems to be 1000.

Finally, you can also use the MostRecentlyUsedList collection in the same namespace to store the files/folder used the most in your application.

 

One Response

Comments are closed.