FileOpenPicker FileOpenPicker FileOpenPicker FileOpenPicker FileOpenPicker Class

Definition

Represents a UI element that lets the user choose and open files.

public : sealed class FileOpenPicker : IFileOpenPicker, IFileOpenPicker2, IFileOpenPickerWithOperationId
struct winrt::Windows::Storage::Pickers::FileOpenPicker : IFileOpenPicker, IFileOpenPicker2, IFileOpenPickerWithOperationId
public sealed class FileOpenPicker : IFileOpenPicker, IFileOpenPicker2, IFileOpenPickerWithOperationId
Public NotInheritable Class FileOpenPicker Implements IFileOpenPicker, IFileOpenPicker2, IFileOpenPickerWithOperationId
var fileOpenPicker = new fileOpenPicker();
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

The File picker sample demonstrates how to check whether the app is snapped, how to set file picker properties, and how to show a file picker so that the user can pick one file.

FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");

StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    OutputTextBlock.Text = "Picked photo: " + file.Name;
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});
internal bool EnsureUnsnapped()
{
    // FilePicker APIs will not work if the application is in a snapped state.
    // If an app wants to show a FilePicker while snapped, it must attempt to unsnap first
    bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
    if (!unsnapped)
    {
        NotifyUser("Cannot unsnap the sample.", NotifyType.StatusMessage);
    }

    return unsnapped;
}

Note

You should always make sure that your app is not snapped (or that it can be unsnapped) and set file picker properties regardless of whether the user is picking a single file or multiple files.

IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
if (files.Count > 0)
{
    StringBuilder output = new StringBuilder("Picked files:\n");
    // Application now has read/write access to the picked file(s)
    foreach (StorageFile file in files)
    {
        output.Append(file.Name + "\n");
    }
    OutputTextBlock.Text = output.ToString();
}
else
{
    OutputTextBlock.Text = "Operation cancelled.";
}
openPicker.pickMultipleFilesAsync().then(function (files) {
    if (files.size > 0) {
        // Application now has read/write access to the picked file(s)
        var outputString = "Picked files:\n";
        for (var i = 0; i < files.size; i++) {
            outputString = outputString + files[i].name + "\n";
        }
        WinJS.log && WinJS.log(outputString, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

Remarks

To get started accessing files and folders file picker, see Quickstart: Accessing files with .

To learn about using file pickers in Windows Phone 8.x app, see How to continue your Windows Phone app after calling a file picker.

Important

In Windows 8 if you attempt to display the file picker while your app is snapped, the file picker will not be shown and an exception will be thrown. You can avoid this by making sure your app is not snapped, or by unsnapping it before you call the file picker. The following code examples and the File picker sample show you how. Note that Windows 8.1 does not define a specific snapped window size. Instead, users can resize apps to any width, down to the minimum. Therefore, if your app will deploy only on Windows 8.1, you can ignore the EnsureUnsnapped function and calls to it in this topic's example code.

Constructors

FileOpenPicker() FileOpenPicker() FileOpenPicker() FileOpenPicker() FileOpenPicker()

Creates a new instance of a FileOpenPicker.

Properties

CommitButtonText CommitButtonText CommitButtonText CommitButtonText CommitButtonText

Gets or sets the label text of the file open picker's commit button.

ContinuationData ContinuationData ContinuationData ContinuationData ContinuationData

Gets a set of values to be populated by the app before PickSingleFileAndContinue or PickMultipleFilesAndContinue operation that deactivates the app in order to provide context when the app is activated. (Windows Phone 8.x app)

FileTypeFilter FileTypeFilter FileTypeFilter FileTypeFilter FileTypeFilter

Gets the collection of file types that the file open picker displays.

SettingsIdentifier SettingsIdentifier SettingsIdentifier SettingsIdentifier SettingsIdentifier

Gets or sets the settings identifier associated with the state of the file open picker.

SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation

Gets or sets the initial location where the file open picker looks for files to present to the user.

ViewMode ViewMode ViewMode ViewMode ViewMode

Gets or sets the view mode that the file open picker uses to display items.

Methods

PickMultipleFilesAndContinue() PickMultipleFilesAndContinue() PickMultipleFilesAndContinue() PickMultipleFilesAndContinue() PickMultipleFilesAndContinue()

Obsolete as of Windows 10; use PickSingleFolderAsync instead. Shows the file picker so that the user can pick multiple files, deactivating and the app and reactivating it when the operation is complete. (Windows Phone 8.x app)

PickMultipleFilesAsync() PickMultipleFilesAsync() PickMultipleFilesAsync() PickMultipleFilesAsync() PickMultipleFilesAsync()

Shows the file picker so that the user can pick multiple files. (UWP app)

PickSingleFileAndContinue() PickSingleFileAndContinue() PickSingleFileAndContinue() PickSingleFileAndContinue() PickSingleFileAndContinue()

Shows the file picker so that the user can pick one file, potentially deactivating the app and reactivating it when the operation is complete. (Windows Phone 8.x app)

PickSingleFileAsync() PickSingleFileAsync() PickSingleFileAsync() PickSingleFileAsync() PickSingleFileAsync()

Shows the file picker so that the user can pick one file.

PickSingleFileAsync(String) PickSingleFileAsync(String) PickSingleFileAsync(String) PickSingleFileAsync(String) PickSingleFileAsync(String)

Shows the file picker so that the user can pick one file.

ResumePickSingleFileAsync() ResumePickSingleFileAsync() ResumePickSingleFileAsync() ResumePickSingleFileAsync() ResumePickSingleFileAsync()

Resumes the picking operation to retrieve the user-selected file if the user navigates away from the app after the picker operation has completed and the app is suspended.

See Also