This is a mirror of official site: http://jasper-net.blogspot.com/

WinRT: File Access

| Tuesday, March 20, 2012
This post contains my notes from the "File Access" sample in the Windows 8 developer samples.

Let me start by noting that all these samples are designed to show how the API is structured.  These samples do not show proper separation of concerns, or architectural patterns like MVVM. In fact, all the code is in the CodeBehind for the MainWindows.xaml.cs.  Oh well, that’s not what the samples are teaching.

There are two key takeaways from this sample.  First, IO programming in WinRT is very similar to IO programming in the .NET Framework. The addition of the ‘await’ keyword before each call is the only new technique here. Second, Metro applications should not just browse the file system for storage locations.  Each application should restrict its storage to the application specific document library.  The code shows both of these concepts pretty clearly.

The main Window’s Initialize() code initializes a StorageFile object by searching for the file on disc, and creating the associated StorageFile object (asynchronously, of course):

async void Initialize()
{
    try
    {
        sampleFile = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync("sample.txt");
    }
    catch (FileNotFoundException)
    {
        // 'sample.txt' doesn't exist so scenario one must be run
    }
}

QR: Inline image 1

Posted via email from Jasper-net

0 comments: