Created
April 15, 2012 05:46
-
-
Save biac/2390305 to your computer and use it in GitHub Desktop.
Demo1Page.SaveTxtAutoButton_Click_1()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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