Thursday, November 24, 2011

WinRT File Access

In my last post I showed how to use an OpenFilePicker in a WinRT application to get access to a file selected by the user. Now let’s take a look at how you access the file system directly without prompting the user. We will start with a simple application that lists the names of the files in the user’s Picture Library.
Start by creating a C# Windows Metro Style Application. Within the Grid element add the following XAML to create textblock we can use to display the name.

<TextBlock x:Name="TextBlock" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top"/>

In the code behind add the following code to handle the UserControl_Loaded event:

private async void UserControl_Loaded(object sender, RoutedEventArgs e)
{

var folder = KnownFolders.PicturesLibrary;
var files = await folder.GetFilesAsync();

foreach (StorageFile file in files)
{ 
    TextBlock.Text += file.FileName + ":" + file.ContentType + "\n"; 
}

}

Before running the code, select Exceptions from the Debug menu and check Common Language Runtime Exceptions Thrown. Now run the application and you will notice that it throws a System.UnauthorizedAccessException. Why does this happen?

As I mentioned in my last post, WinRT apps run in a pretty strict sandbox and by default have very limited access to the file system. We got around this last time because we let the user pick a file, so they would have full knowledge of what they are allowing the application to access. But in this case, since we are directly accessing the file system, we get an exception. To solve this problem, double click Package.appxmamnifest in the solution explorer the click on the Capabilities tab.

clip_image002

On this screen we can specify what we want application to be allowed to do. To give the application access to the Picture Library, put a check next to Picture Library Access. If you then re-run the application you will not get an exception and a list of the pictures will be displayed. At first this might seem a little pointless. If the programmer can just click a check box to get access it’s not particularly good security. The value is that it will make it much easier for the prospective user of the app to know what the app is allowed to do. Although we haven’t seem Microsoft’s Windows 8 app store yet, it’s entirely possible that when we look at the information about an app before we purchase it, we will able to see a list of these Capabilities so we know exactly what the app has access to.

Now that we understand about Capabilities, lets look at how the code actually works. In the first line we create a new folder object from one of the KnownFolders. In this case the code selects the PictureLibrary, but we could also select folders like the MusicLibrary, VideosLibrary, or DocumentLibrary.

Once we have the folder object the second line calls it’s GetFilesAsync method to return a list of StorageFile objects for the files in the folder. As always with WinRT, if a method could potentially take more the 50ms to run, it’s executed asynchronously. The next three lines iterate through the list of StorageFiles and display their names.

1 comment:

Unknown said...

Thank you for a concise explanation,
and a working example.
-Russ