FileSavePicker FileSavePicker FileSavePicker FileSavePicker FileSavePicker Class

Definition

Represents a file picker that lets the user choose the file name, extension, and storage location for a file.

public : sealed class FileSavePicker : IFileSavePicker, IFileSavePicker2, IFileSavePicker3
struct winrt::Windows::Storage::Pickers::FileSavePicker : IFileSavePicker, IFileSavePicker2, IFileSavePicker3
public sealed class FileSavePicker : IFileSavePicker, IFileSavePicker2, IFileSavePicker3
Public NotInheritable Class FileSavePicker Implements IFileSavePicker, IFileSavePicker2, IFileSavePicker3
var fileSavePicker = new fileSavePicker();
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 save a file.

if (rootPage.EnsureUnsnapped())
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    // Dropdown of file types the user can save the file as
    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    // Default file name if the user does not type one in or select a file to replace
    savePicker.SuggestedFileName = "New Document";

    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
    {
        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
        CachedFileManager.DeferUpdates(file);
        // write to file
        await FileIO.WriteTextAsync(file, file.Name);
        // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
        // Completing updates may require Windows to ask for user input.
        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
        if (status == FileUpdateStatus.Complete)
        {
            OutputTextBlock.Text = "File " + file.Name + " was saved.";
        }
        else
        {
            OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
        }
    }
    else
    {
        OutputTextBlock.Text = "Operation cancelled.";
    }
}
// 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 savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.fileTypeChoices.insert("Plain Text", [".txt"]);
// Default file name if the user does not type one in or select a file to replace
savePicker.suggestedFileName = "New Document";

savePicker.pickSaveFileAsync().then(function (file) {
    if (file) {
        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
        Windows.Storage.CachedFileManager.deferUpdates(file);
        // write to file
        Windows.Storage.FileIO.writeTextAsync(file, file.name).done(function () {
            // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
                if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
                    WinJS.log && WinJS.log("File " + file.name + " was saved.", "sample", "status");
                } else {
                    WinJS.log && WinJS.log("File " + file.name + " couldn't be saved.", "sample", "status");
                }
            });
        });
    } else {
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});
FileSavePicker^ savePicker = ref new FileSavePicker();
savePicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;

auto plainTextExtensions = ref new Platform::Collections::Vector<String^>();
plainTextExtensions->Append(".txt");
savePicker->FileTypeChoices->Insert("Plain Text", plainTextExtensions);
savePicker->SuggestedFileName = "New Document";

create_task(savePicker->PickSaveFileAsync()).then([this](StorageFile^ file)
{
    if (file != nullptr)
    {
        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
        CachedFileManager::DeferUpdates(file);
        // write to file
        create_task(FileIO::WriteTextAsync(file, file->Name)).then([this, file]()
        {
            // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
            // Completing updates may require Windows to ask for user input.
            create_task(CachedFileManager::CompleteUpdatesAsync(file)).then([this, file](FileUpdateStatus status)
            {
                if (status == FileUpdateStatus::Complete)
                {
                    OutputTextBlock->Text = "File " + file->Name + " was saved.";
                }
                else
                {
                    OutputTextBlock->Text = "File " + file->Name + " couldn't be saved.";
                }
            });
        });
    }
    else
    {
        OutputTextBlock->Text = "Operation cancelled.";
    }
});
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;
}

Remarks

To learn how to save files through the file picker, see How to save files through file pickers.

To get started accessing files and folders file picker, see Files, folders, and libraries .

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.

Constructors

FileSavePicker() FileSavePicker() FileSavePicker() FileSavePicker() FileSavePicker()

Creates a new instance of a FileSavePicker.

Properties

CommitButtonText CommitButtonText CommitButtonText CommitButtonText CommitButtonText

Gets or sets the label text of the commit button in the file picker UI.

ContinuationData ContinuationData ContinuationData ContinuationData ContinuationData

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

DefaultFileExtension DefaultFileExtension DefaultFileExtension DefaultFileExtension DefaultFileExtension

Gets or sets the default file name extension that the fileSavePicker gives to files to be saved.

EnterpriseId EnterpriseId EnterpriseId EnterpriseId EnterpriseId

Gets or sets an ID that specifies the enterprise that owns the file.

FileTypeChoices FileTypeChoices FileTypeChoices FileTypeChoices FileTypeChoices

Gets the collection of valid file types that the user can choose to assign to a file.

SettingsIdentifier SettingsIdentifier SettingsIdentifier SettingsIdentifier SettingsIdentifier

Gets or sets the settings identifier associated with the current FileSavePicker instance.

SuggestedFileName SuggestedFileName SuggestedFileName SuggestedFileName SuggestedFileName

Gets or sets the file name that the file save picker suggests to the user.

SuggestedSaveFile SuggestedSaveFile SuggestedSaveFile SuggestedSaveFile SuggestedSaveFile

Gets or sets the storageFile that the file picker suggests to the user for saving a file.

SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation SuggestedStartLocation

Gets or sets the location that the file save picker suggests to the user as the location to save a file.

Methods

PickSaveFileAndContinue() PickSaveFileAndContinue() PickSaveFileAndContinue() PickSaveFileAndContinue() PickSaveFileAndContinue()

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

PickSaveFileAsync() PickSaveFileAsync() PickSaveFileAsync() PickSaveFileAsync() PickSaveFileAsync()

Shows the file picker so that the user can save a file and set the file name, extension, and location of the file to be saved. (UWP app)

See Also