User Tools

Site Tools


notes:uwp:pickers

Pickers in UWP

Properties common to all pickers:

  • SuggestedStartLocation (PickerLocationId)
  • CommitButtonText
  • SettingsIdentifier

FileOpenPicker

There are two methods to pick files using FileOpenPicker:

  • PickSingleFileAsync
  • PickMultipleFilesAsync

Example: Pick a single file:

using Windows.Storage;
using Windows.Storage.Pickers;
...
var picker = new FileOpenPicker();
 
// Add at least one file type to the FileTypeFilter collection.
picker.FileTypeFilter.Add(".txt");
 
// Display the FileOpenPicker. 
// The FileOpenPicker.PickSingleFileAsync method returns the selected file if the user clicks OK.
// If the user clicked Cancel, the returned file from the FileOpenPicker is null.
StorageFile file = await picker.PickSingleFileAsync();
 
if (file != null)
{
    // the app has read/write access to the file
    // ... do something with the file
}

Example: Pick a file from the Pictures Library:

using Windows.Storage;
using Windows.Storage.Pickers;
...
var picker = new FileOpenPicker();
 
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".bmp");
 
picker.ViewMode = PickerViewMode.Thumbnail; // an optional setting
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
 
StorageFile file = await picker.PickSingleFileAsync();
 
// Show the picker.
if (file != null)
{
    // the app has read/write access to the file
}
else
{
    // the user cancelled the operation
}

FileSavePicker

Properties specific to FileSavePicker:

  • FileTypeChoices
  • SuggestedFileName
  • SuggestedSaveFile
  • DefaultFileExtension

Example: Prompt the user for a file:

using Windows.Storage;
using Windows.Storage.Pickers;
...
var picker = new FileSavePicker();
 
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.SuggestedFileName = "New Document"; // default filename
picker.FileTypeChoices.Add("Text", new List<string> { ".txt" });
picker.DefaultFileExtension = ".txt";
 
// Show the picker.
StorageFile file = await picker.PickSaveFileAsync();
 
if (file != null) // the file is null if the user clicked Cancel
{
    // ... do something with the file
}
using Windows.Storage;
using Windows.Storage.Pickers;
...
var picker = new FileSavePicker();
 
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.SuggestedFileName = "New Image"; // default filename
picker.FileTypeChoices.Add("JPEG files", new List<string> { ".jpg" });
 
// Show the picker.
StorageFile file = await picker.PickSaveFileAsync();
 
if (file != null) // the file is null if the user clicked Cancel
{
    // ... do something with the file
}

FolderPicker

Example: Allow the user to pick a folder. The folder refers to the user-selected StorageFolder object:

using Windows.Storage;
using Windows.Storage.Pickers;
...
var picker = new FolderPicker { FileTypeFilter = {"*"} }; 
StorageFolder folder = await picker.PickSingleFolderAsync(); 
if (folder == null) return; // the folder is null if the user clicked Cancel
notes/uwp/pickers.txt · Last modified: 2018/01/18 by leszek