Home > Metro, Windows 8 > Windows 8 New UI Apps FilePicker

Windows 8 New UI Apps FilePicker

Every Win 8 app does live in its own sandbox. Communication between apps is very limited.

The following picture does show the possible interaction ways:

image

In theory, any developer can require access to the users filesystem places by checking the matching capabilities in the app manifest.

But this will ask the user on the first start, whether he want to allow access the app to this specific places.

And: Some of the capabilities can only be requested by developers, who have registered a company account!

The reasons, that this is in almost all the cases not a very good idea, is data privacy. If a user allows access to his data folders, then the app could transfer all data to any place in the internet.

==> Capabilities for allowing access to the users data locations should be used very seldom due to potential data privacy issues!

image

But even in the scenario, that the user will allow access to this places:

The app will never be able to transfer data to or from the users desktop or any other filesystem location, because the access is limited to the e.g. Documents libarary.

 

If your app needs to transfer data to any location on the users filesystem, then the concept of FilePicker will be very helpful.

FileOpenPicker and FileSavePicker are a way to ask the user to provide a file. The user can then navigate to any place on the local filesystem or even on any connected network filesystem and select a file.

Using FileOPenPicker the user will see this and can switch between all this places:

image                                                           image

The big difference then is, that the user is in charge and can decide whether he really wants to allow the app to access this file.

The app can never access this file without the users explicit action.

There is even another big advantage using the FilePicker interface:

It’s not only limited to the local and network filesystem, but can access a lot of other places, like Skydrive and other third party apps. You can even provide a FilePicker interface in your app, which then acts as source for data read/write operations to other apps.

Another very nice feature of this interface is, that reading and writing to any source/destination which the user has selected, is as easy as reading from a local file. There is no difference and the app code must not handle any special cases. It is just reading or writing through the FileOpen- or FileSavePicker interface.

Using the FileOpenPicker from code requires only a few lines of code:

private async void LoadFileUsingFilePicker()
{
    FileOpenPicker fop = new FileOpenPicker();
    fop.SuggestedStartLocation = PickerLocationId.ComputerFolder;
    fop.ViewMode = PickerViewMode.List;
    fop.FileTypeFilter.Add(".txt");
    fop.FileTypeFilter.Add(".cs");
    var storageFile = await fop.PickSingleFileAsync();
    if (storageFile != null)
    {
        var lines = await FileIO.ReadLinesAsync(storageFile);
        if (lines != null)
        {
            for (int i = 0; i < lines.Count; i++)
            {
                txtData.Text += lines[i];
            }
        }

    }
}


The same applies for saving a file to any location on the users filesystem

(but on behalf of the user):

private async void SaveFileUsingFilePicker()
{
    FileSavePicker fsp = new FileSavePicker();
    fsp.SuggestedStartLocation = PickerLocationId.Desktop;
    fsp.FileTypeChoices.Add("Text file", new string[] { ".txt" });
    fsp.SuggestedFileName = "NewFile.txt";
    fsp.DefaultFileExtension = ".txt";
    StorageFile storageFile = await fsp.PickSaveFileAsync();
    if (storageFile != null)
    {
        await FileIO.WriteTextAsync(storageFile, txtData.Text);
    }
}
Categories: Metro, Windows 8
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: