Skip to content

Instantly share code, notes, and snippets.

@biac
Created April 15, 2012 03:16
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/2389711 to your computer and use it in GitHub Desktop.
Save biac/2389711 to your computer and use it in GitHub Desktop.
Demo1Page.OpenTxtButton_Click_1()
private async void OpenTxtButton_Click_1(object sender, RoutedEventArgs e)
{
var thisButton = sender as Button;
thisButton.IsEnabled = false;
// FileOpenPicker を用意する
var picker = new FileOpenPicker();
picker.CommitButtonText = "テキストファイルを開く";
picker.FileTypeFilter.Add(".txt"); // 1つは必須!!
picker.FileTypeFilter.Add(".cs");
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
picker.ViewMode = PickerViewMode.List;
// FileOpenPicker を出して、ファイルを選択してもらう 【非同期!】
StorageFile file = await picker.PickSingleFileAsync();
// 選択されたファイルの内容を TextBox に読み込む
if (file != null) {
using(Stream s = await file.OpenStreamForReadAsync()) // 選択してもらった StorageFile から Stream を作る 【非同期!】
using(StreamReader r = new StreamReader(s)) // Stream から StreamReader を作る
this.TextArea.Text = await r.ReadToEndAsync(); // StreamReader にファイルを尻尾まで読み込んでもらう 【非同期!】
}
thisButton.IsEnabled = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment