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);
}
<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>
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);
}
}
}