Libraries continue to play important role in Windows Store app development, even though they are hidden by default in Windows 8.1. Moreover, a new class called StorageLibrary is introduced in WinRT to help managing user libraries.

StorageLibrary provides methods and events to add and delete folders within the libraries, to monitor library changes. It supports Documents, Music, Pictures and Videos libraries. Custom libraries are not supported and apps have to declare corresponding capabilities to access standard libraries.

How to add new folder to Pictures library


// Get StorageLibrary object, throws exception when Pictures library capability isn't declared
StorageLibrary library = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
   
// Ask user to select folder to add
StorageFolder newFolder = await library.RequestAddFolderAsync();

RequestAddFolderAsync() shows a new specialized file picker where users can select folder to add to Pictures library:

Add folder picker

In this case, SkyDrive camera roll folder was selected:

Libraries

How to remove folder from Pictures library

StorageLibrary.RequestRemoveFolderAsync() let applications to remove folders from the libraries. Unlike RequestAddFolderAsync(), RequestRemoveFolderAsync() does not provide any UI. Developers have to implement folder selection logic by themselves, for example by using folder picker or StorageLibrary.Folders.

The following code demonstrates removing of the SkyDrive camera roll folder added in the previous example:


StorageLibrary library = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);

StorageFolder folder = library.Folders.FirstOrDefault(x => x.Name == "SkyDrive camera roll")

if (folder != null)
{
   bool deleted = await library.RequestRemoveFolderAsync(folder);
   // …
}

RequestRemoveFolderAsync method displays the following question:

Remove folder

and removes the folder if action is approved.

Removed folder

Monitoring libraries

Since the selected library can be modifies outside of app (by user or another app), StorageLibrary provides DefinitionChanged event to let applications to track changes in the list of folders.

In addition, StorageLibrary.SaveFolder provides the default save folder for the library.

blog comments powered by Disqus