共用方式為


How to save files through file pickers (XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Use FileSavePicker to let users specify the name and location where they want to save your app’s content.

Prerequisites

Understand async programming for Windows Runtime apps using C++, C#, or Visual Basic

You can learn how to write asynchronous apps in Quickstart: Calling asynchronous APIs in C# or Visual Basic.

Instructions

Step 1: Create and customize the FileSavePicker

Use FileSavePicker so that your user can specify the name, file type, and location of a file to save. You can customize the file picker by setting properties on the FileSavePicker you create.

The File picker sample demonstrates how to create and customize a FileSavePicker object.

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

You should set FileSavePicker properties that are relevant to your users and your app. For guidelines to help you decide how to customize the file picker, see Guidelines and checklist for file pickers.

The File picker sample customizes the file picker by setting three FileSavePicker properties: SuggestedStartLocation, FileTypeChoices and SuggestedFileName properties.

Customizing a FileSavePicker, in detail

  1. Because our user is saving a document or text file, the sample sets savePicker.SuggestedStartLocation to the app's local folder by using LocalFolder.

    savePicker.SuggestedStartLocation = Windows.Storage.ApplicationData.LocalFolder;
    

    You should set SuggestedStartLocation to file system location that is appropriate for the type of file that is being picked. If the user is picking music, pictures, or videos, set the start location to Music, Pictures, or Videos respectively. For all other types of files, set the start location to the app's local folder. This is just a starting location. Users can navigate to other locations while they are using the file picker.

  2. Because we want to make sure our app can open the file after it is saved, the sample uses savePicker.FileTypeChoices.Add to specify file types that the sample supports (Microsoft Word documents and text files).

    Add is the Append method as it is projected for C# developers.

    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    

    Make sure all the file types that you specify are supported by your app.

    Users will be able to save their file as any of the file types you specify in this list. They can also change the file type by selecting a different file type (that you specified) from the drop-down control in the lower right of the file picker. The first file-type choice in the list will be selected by default. To change which file type is selected by default, set the savePicker.DefaultFileExtension property.

    Note  The file picker also uses the currently selected file type to filter which files it displays, so that only file types that match the selected files types are displayed to the user.

     

  3. Because we want to try to save the user some typing, the sample sets a savePicker.SuggestedFileName for the file to save.

    savePicker.SuggestedFileName = "New Document";
    

    Try to make your suggested file name as relevant as possible to the file that your user is saving. For example, like Word, you can suggest the existing file name if there is one, or the first line of a document if the user is saving a file that does not yet have a name.

Note  FileSavePicker objects display the file picker using the PickerViewMode.List.

 

Step 2: Show the FileSavePicker to save a file

After you create and customize the file picker, let the user save the file by calling savePicker.PickSaveFileAsync.

After the user specifies the name, file type, and location, and confirms to save the files, PickSaveFileAsync returns a StorageFile object that represents the saved file. You can capture and process this file by using the await operator.

The File picker sample demonstrates how to display the file picker to let the user save a file.

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

After the file is saved, the app has read and write access to that file. The sample captures the saved file in the file parameter and checks that the file is valid, but the sample doesn't actually process the file further. You can learn how to write to and read from the resulting file in Quickstart: Reading and writing a file.

Tip  You should always check the saved file (file in the sample) to make sure it is valid before you perform any other processing. Then, you can save content to the file as appropriate for your app, and provide appropriate behavior if the picked file is not valid.

 

Remarks

If you use code similar to what is shown here, your app should display the file picker to your users and let them specify the name, file type, and location for a file to save. You can learn how to write to and read from the resulting file in Quickstart: Reading and writing a file.

If you want to add "save as" to your app, add a control to your app's UI that calls the FileSavePicker. To learn about adding controls, see Adding controls and content.

If you want your app to provide a save location to users through the file picker, see Quickstart: Integrating with file picker contracts.

File picker sample

File access sample

Guidelines and checklist for file pickers

Quickstart: Accessing files with file pickers

Quickstart: Reading and writing a file

Reference

Windows.Storage.Pickers namespace

Windows.Storage.Pickers.FileSavePicker class

Windows.Storage.StorageFile class

File picker contracts

Integrating with file picker contracts

Quickstart: Integrating with file picker contracts

Guidelines and checklist for file picker contracts