Sunday 22 February 2015

Chapter Five : File Pickers

MSFT introduces File picker to pick files & folders from our computer. Let’s emphasize over this file picker usage through C# code in our metro style app.
Let’s commence to learn happily with me :)

File Picker:

  • Access files, folders, and data from your Windows Store app.
  • File pickers give users explicit control over which files and folders your app can access, and give you an easy way to let users specify the name or location of a file to save. If your app works with individual files or small lists of files, like most apps, these file picker interactions may be all you need to access files and folders for your app.

For example, in this screen shot an email app called the file picker so that the user could choose a picture from the Pictures library.
 And in this screen shot the Reader app called the file picker so that the user could specify the file name and location of the file to save.

For selecting a single file from the user’s hard drive, we are going to start with the FileOpenPicker object.  This is the object that will open the File Picker dialog for the user, help them select one or many files, and return them to us
FileOpenPicker picker = newFileOpenPicker();
picker.FileTypeFilter.Add(".xls");
StorageFile file = await picker.PickSingleFileAsync();


FileOpenPicker picker = newFileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".gif");
picker.FileTypeFilter.Add(".bmp");
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
StorageFile file = await picker.PickSingleFileAsync();

This time the File Picker will launch directly to the location you’ve specified (in my example, it is the Pictures Library):
 
The last option we can manipulate relates to how the File Picker will display the files.  Simply use the ViewMode property of the FileOpenPicker object, like this:
picker.ViewMode = PickerViewMode.List;

List:


Thumbnail:

Retrieving Multiple Files From Your User’s Computer
Sometimes, we want to grab more than one file at a time.  In this case, we can use a different method call on our FileOpenPicker object.  This time, we’ll use the PickMultipleFilesAsync() method.
FileOpenPicker picker = newFileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".bmp");
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.ViewMode = PickerViewMode.Thumbnail;
IReadOnlyList<StorageFile> files = await picker.PickMultipleFilesAsync();

You’ll find that if the file already exists, you’ll also get prompted with a “Replace the existing file?” dialog box.

Ultimately, that’s all it takes to save a simple file to the user’s system.  For more complex operations and file types, you’re going to want to explore the FileIO class.  You can read more about that on MSDN.
Selecting a Folder on the User’s Machine
This is great for allowing the user to choose a default save location for the future, or perhaps a place for you to look for files in the future. 
FolderPicker picker = newFolderPicker();
picker.FileTypeFilter.Add(".xls");
StorageFolder folder = await picker.PickSingleFolderAsync();
if (folder != null)
{                                                                 
StorageApplicationPermissions.FutureAccessList.AddOrReplace("DefaultFolder", folder);
}

By:
Khushbu Wadhwani,
Senior Windows app developer,
References:MSDN,Microsoft developer forum

No comments:

Post a Comment