FileSavePicker in Windows Store App Using C#

Today we learn about the FileSavePicker control in a Windows 8 Apps using C#. FileSavePicker allows users to specify the name and location of where they want to save your app's content. You can also customize the file picker by setting properties on the FileSavePicker according to their need.

Before we start work with the FileSavePicker have a look at the Properties and Methods of SaveFilePicker.

Properties:

  • SuggestedStartLocation: Sets the location that the file suggests to the user to save a file.
  • FileTypeChoices: It lets the user select the file types from the Dropdown list.
  • SuggestedFileName: It is used for the Default file name if the user does not type one in or select a file to replace.
  • DefaultFileExtension: Used for the Default extension if the user does not select a choice explicitly from the dropdown.
  • CommitButtonText: Used for a customized commit button that is displayed to save.
  • SuggestedSaveFile: Gets or Sets the Storage File that the file suggests to the user to save the file.

Methods:

  • PickSaveFileAsync():  Lets the user save the file.

Now, we create an application to show how to use the FileSavePicker.

In the MainPage.XAML file we take a button that is used to open the FileSavePicker and a textblock to show the status message.

XAML Code:

<Page

    x:Class="filesavepicker.MainPage"

    IsTabStop="false"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:filesavepicker"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="#FF318083">

        <StackPanel Orientation="Vertical" Margin="40,50">

            <Button x:Name="save" Content="save" Click="save_Click_1" Width="94"></Button>

            <TextBlock x:Name="status"  FontSize="30"/>

        </StackPanel>

    </Grid>

</Page>

In the MainPage.XAML.cs  include the following Namespaces:

using Windows.Storage.Pickers;
using Windows.Storage;
 

In the MainPage.XAML.cs file create an event handler and customize the FileSavePicker by using the preceding properties and methods.

Code of MainPage.XAML.cs file:
 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.Storage.Pickers;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.Storage;

using Windows.Storage.Streams;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

 

namespace filesavepicker

{

    /// <summary>

    /// An empty page that can be used on its own or navigated to within a Frame.

    /// </summary>

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

 

        /// <summary>

        /// Invoked when this page is about to be displayed in a Frame.

        /// </summary>

        /// <param name="e">Event data that describes how this page was reached.  The Parameter

        /// property is typically used to configure the page.</param>

        protected async override void OnNavigatedTo(NavigationEventArgs e)

        {

        }

 

        private async void save_Click_1(object sender, RoutedEventArgs e)

        {

            FileSavePicker savePicker = new FileSavePicker();

            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

            // Dropdown of file types the user can save the file a

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

            // Default extension if the user does not select a choice explicitly from the dropdown

            savePicker.DefaultFileExtension = ".docx";

            // 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 (null != file)

            {

                // Application now has read/write access to the saved file

                await Windows.Storage.FileIO.WriteTextAsync(file, "Hello, This is text file");

                status.Text = "Your File Successfully Saved";

            }

            else

            {

                status.Text = "File was not returned";

            }

        }

    }

}


Now, build the application and Run by using F5. Click on the Save button. It opens the FileSavePicker.

FileSavePicker-Control-In-Windows8-Apps.png

Give the file name and choose the location of where you want to save your file. Then, click the save button to save the file.

FileSavePicker-In-Windows8-Apps.png

You get the message of saving the file successfully. Go to the location and open it.

Saving-File-In-Windows8-Apps.png

Summary: In the article we learned how to save a file in a Windows 8 Apps to a particular location and also write the content to it.

Up Next
    Ebook Download
    View all
    Learn
    View all