Dela via


How to track recently used files and folders (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Track files that your user accesses frequently by adding them to your app's most recently used list. The platform manages the MostRecentlyUsedList for you by sorting items based on when they were last accessed, and by removing the oldest item when the list's 25-item limit is reached. All apps have their own MostRecentlyUsedList.

Typically, your app's most recently used list (MRU) will be most useful for tracking your user's most recently used files, but the MRU can also be used to track folders. You can store both files and folders in your app's most recently used list (MRU). Items are stored as IStorageItem objects. This means that StorageFile objects (which represent files) and StorageFolder objects (which represent folders) can both be added to the MRU.

Prerequisites

File access and permissions

Explains which files and locations your app has access to by default, and the ways your app can gain access to additional files and locations.

Quickstart: Accessing files with file pickers

Explains how to let users pick the files for your app to operate on. Picked files are often the same files that users return to again and again.

File access sample

File picker sample

Add picked files to the MRU

The files that your user picks are often files that he or she returns to over and over again. Because of this, if your user picks a file, you should strongly consider adding that file to your app's most recently used list (MRU). You can easily add picked files to the MRU as soon as they are picked, by following these steps.

  1. Find the code in your app that lets your user pick files and/or folders.

    If you're not sure where this code is or if you don't understand how to use the file picker to access files, read Quickstart: Accessing files with file pickers before you continue.

    For example, if you let the user pick a single file, your code should look similar to this:

    using Windows.Storage;
    using Windows.Storage.Pickers;
    
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.ReplaceAll([".png", ".jpg", ".jpeg"]);
    
    // Open the picker for the user to pick a file
    StorageFile pickedFile = await openPicker.PickSingleFileAsync();
    

    When the asynchronous call returns, PickSingleFileAsync in the example, the file that the user picked is returned as a StorageFile that is stored in the pickedFile variable.

    If you are letting your user pick a folder, instead of a file, your code uses PickSingleFolderAsync. The picked folder is then returned as a StorageFolder.

  2. Add the picked file to the most recently used list (MRU) by adding a line of code like this after your asynchronous call:

    String mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(pickedFile, "profile pic");
    

    MostRecentlyUsedList.Add is overloaded. In the example, we use Add(IStorageItem, String) so that we can associate metadata with the file. Setting metadata lets you capture additional information, like the item's purpose. In the example, we capture the purpose of the file by setting the metadata to "profile pic". You can also add the file to the MRU without metadata by calling Add(IStorageItem).

    Whenever you Add an item to the MRU, the method returns a uniquely identifying string, called a token, that is used to retrieve the item. In the example, we capture this token in the mruToken local variable, but we don't do anything else with it.

    Tip   Because you need to use a token to retrieve an item from the MRU, you should consider saving the token to app data for subsequent use. For example, if your app adds a profile pic to the MRU (like we do in the example), you might want to save the token to a list in app data where you track the user's profiles pictures. You can learn more about app data in Managing application data.

     

    Your code should now look similar to this:

    // Open the picker for the user to pick a file
    StorageFile pickedFile = await openPicker.PickSingleFileAsync();
    // Add picked file to MRU
    String mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(pickedFile, pickedFile.Name);
    

Use tokens to retrieve items from the MRU

To retrieve an item from the MostRecentlyUsedList you need the token for that item. You should use the retrieval method most appropriate for the item you want to retrieve:

For example, we get the token for the first item, a file, in our MRU and then use the token to retrieve a StorageFile that represents that file, with this code:

using Windows.Storage;
using Windows.Storage.AccessCache;

String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First.Token;
StorageFile retrievedFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruFirstToken);

In the example, we know that the first item is a file because we have only added one file to the MostRecentlyUsedList.

Retrieve tokens for all items in the MRU

You can retrieve the token for every item in the list by iterating the entries like this:

using Windows.Storage.AccessCache;

AccessListEntryView mruEntries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;
if (mruEntries.Size > 0) {
    foreach (AccessListEntry entry in mruEntries) {
        String mruToken = entry.Token;
        // Continue processing the MRU entry
    }
} else {
    // Handle empty MRU
}

The AccessListEntryView lets you iterate entries in the MRU. These entries are AccessListEntry structures that contain the token and metadata for an item. Entries do not contain the item itself and can't be used to retrieve the associated item directly.

Instead, you can use the AccessListEntry to retrieve the token for an associated item, and then retrieve an item by using one of the methods listed in the preceding section, Use tokens to retrieve items from the MRU. In the example, we capture the token for each entry in the mruToken local variable, but we don't do anything else with it.

The metadata stored in an AccessListEntry is a string with additional information about the item. You can specify the information when you add an item to the MostRecentlyUsedList.

Remarks

Removing items from the MRU when the 25-item limit is reached

When the MRU's 25-item limit is reached and you try to add a new item, the oldest item (the item that was last accessed the longest time ago) is automatically removed. You therefore don't need to remove an item before you add a new one, even if the MRU contains the maximum number of items (25).

Preserving access to files and folders beyond the 25-item limit

In addition to the most recently used list, your app also has a future access list (FutureAccessList) that you can use to maintain your app's access to files and folders that might not be accessible otherwise.

When the user picks a file or folder you should consider adding that item to both your app's MostRecentlyUsedList and FutureAccessList. Adding the file or folder to the FutureAccessList helps ensure that your app maintains access to the item even if the user does not return to it frequently.

How you work with the FutureAccessList is different from how you work with the MRU in a couple of key ways:

  • The FutureAccessList can hold up to 1000 items.
  • Unlike the MRU, the platform does not manage the FutureAccessList for you. When you reach the 1000 item limit, you must remove an item from the list before adding another one. You can do this by using the Remove method.

All apps have their own MostRecentlyUsedList and FutureAccessList by default.