Skip to content

Instantly share code, notes, and snippets.

@biac
Created April 15, 2012 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biac/2390062 to your computer and use it in GitHub Desktop.
Save biac/2390062 to your computer and use it in GitHub Desktop.
Demo1Page.SaveTxtButton_Click_1()
private async void SaveTxtButton_Click_1(object sender, RoutedEventArgs e)
{
var thisButton = sender as Button;
thisButton.IsEnabled = false;
// FileSavePicker を用意する
var picker = new FileSavePicker ();
picker.CommitButtonText = "テキストファイルを保存する";
picker.DefaultFileExtension = ".txt";
picker.FileTypeChoices.Add("TEXT", new string[]{".txt", ".csv"});
picker.FileTypeChoices.Add("C# SRC", new string[] { ".cs", ".xaml" });
picker.SuggestedStartLocation = PickerLocationId.Desktop;
picker.SuggestedFileName = "test.txt";
// FileSavePicker を出して、ファイルを指定してもらう 【非同期!】
StorageFile file = await picker.PickSaveFileAsync();
if (file != null) {
using(Stream s = await file.OpenStreamForWriteAsync()) // Stream を作る 【非同期!】
using(StreamWriter w = new StreamWriter(s, System.Text.Encoding.UTF8)) // Stream から StreamWriter を作る
await w.WriteAsync(this.TextArea.Text); // StreamWriter で書き込む 【非同期!】
}
thisButton.IsEnabled = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment