Skip to content

Instantly share code, notes, and snippets.

@biac
Created April 15, 2012 05:46
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/2390305 to your computer and use it in GitHub Desktop.
Save biac/2390305 to your computer and use it in GitHub Desktop.
Demo1Page.SaveTxtAutoButton_Click_1()
private async void SaveTxtAutoButton_Click_1(object sender, RoutedEventArgs e)
{
var thisButton = sender as Button;
thisButton.IsEnabled = false;
// "My Document" に "AutoSaved.txt" という名前で勝手に保存
StorageFolder myDoc = KnownFolders.DocumentsLibrary; // ←System.UnauthorizedAccessException が発生!!
// Package.appxmanifest に次の設定が必要:
// Capability(「機能」)に「ドキュメント ライブラリ」 (←設定チャームで確認できる)
// Declaration(宣言)に、「ファイルの種類の関連付け」(.txt)
StorageFile file = await myDoc.CreateFileAsync("AutoSaved.txt", CreationCollisionOption.ReplaceExisting); //【非同期!】
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