Represents a UI element that lets the user choose and open files.
Syntax
var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();
Attributes
- ActivatableAttribute(NTDDI_WIN8)
- MuseAttribute()
- VersionAttribute(NTDDI_WIN8)
Members
The FileOpenPicker class has these types of members:
Constructors
The FileOpenPicker class has these constructors.
| Constructor | Description |
|---|---|
| FileOpenPicker | Creates a new instance of a FileOpenPicker. |
Methods
The FileOpenPicker class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.
| Method | Description |
|---|---|
| PickMultipleFilesAsync | Shows the file picker so that the user can pick multiple files. |
| PickSingleFileAsync() | Shows the file picker so that the user can pick one file. |
| PickSingleFileAsync(String) | Windows Phone only. Shows the file picker so that the user can pick one file. The specified operation ID allows apps to resume the picker operation. |
Properties
The FileOpenPicker class has these properties.
| Property | Access type | Description |
|---|---|---|
| Read/write | Gets or sets the label text of the file open picker's commit button. | |
| Read-only | Gets the collection of file types that the file open picker displays. | |
| Read/write | Gets or sets the settings identifier associated with the state of the file open picker. | |
| Read/write | Gets or sets the initial location where the file open picker looks for files to present to the user. | |
| Read/write | Gets or sets the view mode that the file open picker uses to display items. |
Remarks
To get started accessing files and folders file pickers, see Quickstart: Accessing files with file pickers.
Warning If you try to show 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.
Windows Phone 8
This API is supported in native apps only.
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.
// Verify that we are currently not snapped, or that we can unsnap to open the picker var currentState = Windows.UI.ViewManagement.ApplicationView.value; if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped && !Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) { // Fail silently if we can't unsnap return; } // 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"); } });
For C#, the File picker sample demonstrates how to check whether your app is snapped in the EnsureUnsnapped method.
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; }
The File picker sample also demonstrates how to show a file picker so that the user can pick multiple files.
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.
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"); } });
Requirements
|
Minimum supported client | Windows 8 [Windows Store apps only] |
|---|---|
|
Minimum supported server | Windows Server 2012 [Windows Store apps only] |
|
Minimum supported phone | Windows Phone 8 |
|
Namespace |
|
|
Metadata |
|
See also
Build date: 2/25/2013