FileOpenPicker class

4 out of 12 rated this helpful - Rate this topic

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

Syntax


public sealed class FileOpenPicker : Object

Attributes

ActivatableAttribute(NTDDI_WIN8)
MuseAttribute()
VersionAttribute(NTDDI_WIN8)

Members

The FileOpenPicker class has these types of members:

Constructors

The FileOpenPicker class has these constructors.

ConstructorDescription
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.

MethodDescription
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.

PropertyAccess typeDescription

CommitButtonText

Read/writeGets or sets the label text of the file open picker's commit button.

FileTypeFilter

Read-onlyGets the collection of file types that the file open picker displays.

SettingsIdentifier

Read/writeGets or sets the settings identifier associated with the state of the file open picker.

SuggestedStartLocation

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

ViewMode

Read/writeGets 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.


if (rootPage.EnsureUnsnapped())
{
    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.";
    }
}


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.


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.";
}


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

Windows.Storage.Pickers
Windows::Storage::Pickers [C++]

Metadata

Windows.winmd

See also

File picker sample
Quickstart: Accessing files with file pickers

 

 

Build date: 2/25/2013

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.