Sunday 1 February 2015

File Save/Open Picker

previously we went over how to store app and user data in both settings dictionaries and files, but what if we want to give our user the choice of which file they want to open or where to close one? In that case we have to utilize two things called a FileOpenPicker and a FileSavePicker like so

async private void SaveAS_BTN_Click(object sender, RoutedEventArgs e)
{
    var fsp = new FileSavePicker();
    fsp.FileTypeChoices.Add("Text", new List<string>(new string[]{".txt"} ));
           
    StorageFile sf = await fsp.PickSaveFileAsync();

    await FileIO.WriteTextAsync(sf, MyText.Text);
}

async void Open_BTN_Click(object sender, RoutedEventArgs e)
{
    var fop = new FileOpenPicker();
    fop.FileTypeFilter.Add(".txt");
    StorageFile sf = await fop.PickSingleFileAsync();

    MyText.Text = await FileIO.ReadTextAsync(sf);

}

and that`s pretty much it, for completeness here`s our xaml

<Page
    x:Class="pc.FilePickerExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:pc.FilePickerExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
       
        <StackPanel Orientation="Horizontal" Margin="50 50 50 10">
            <Button x:Name="SaveAS_BTN" Content="Save As"/>
            <Button x:Name="Open_BTN" Content="Open" />
        </StackPanel>
        <TextBox x:Name="MyText" TextWrapping="Wrap" Margin="50 10 50 50" Grid.Row="1" />
    </Grid>
</Page>


and our full codebehind

using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace pc.FilePickerExample
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.SaveAS_BTN.Click += SaveAS_BTN_Click;
            this.Open_BTN.Click += Open_BTN_Click;
        }

        async private void SaveAS_BTN_Click(object sender, RoutedEventArgs e)
        {
            var fsp = new FileSavePicker();
            fsp.FileTypeChoices.Add("Text", new List<string>(new string[] { ".txt" }));
            StorageFile sf = await fsp.PickSaveFileAsync();

            await FileIO.WriteTextAsync(sf, MyText.Text);
        }

        async void Open_BTN_Click(object sender, RoutedEventArgs e)
        {
            var fop = new FileOpenPicker();
            fop.FileTypeFilter.Add(".txt");
            StorageFile sf = await fop.PickSingleFileAsync();

            MyText.Text = await FileIO.ReadTextAsync(sf);
        }
    }
}


and that`s it