私はこのチュートリアルhttp://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698%28v=vs.105%29.aspxに従っています。
これまで探していたものですが、唯一の問題は、アプリを閉じて再度開くと、ファイルとテキストが保存されなくなったため、ファイルをテキストとともに永久に保存したいということです。
ここに保存したいhttp://gyazo.com/82e838cd2385cea7021647a8d39f49a8.png/level/batlevel.txt。そのため、アプリを再度開くと、そこに書かれていたテキストがそこに表示されます
private async void btnWrite_Click(object sender, RoutedEventArgs e)
{
await WriteToFile();
// Update UI.
this.btnWrite.IsEnabled = false;
this.btnRead.IsEnabled = true;
}
private async Task WriteToFile()
{
// Get the text data from the textbox.
byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text.ToCharArray());
// Get the local folder.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// Create a new folder name DataFolder.
var dataFolder = await local.CreateFolderAsync("level",
CreationCollisionOption.OpenIfExists);
// Create a new file named DataFile.txt.
var file = await dataFolder.CreateFileAsync("level.txt",
CreationCollisionOption.ReplaceExisting);
// Write the data from the textbox.
using (var s = await file.OpenStreamForWriteAsync())
{
s.Write(fileBytes, 0, fileBytes.Length);
}
}
private async void btnRead_Click(object sender, RoutedEventArgs e)
{
await ReadFile();
// Update UI.
this.btnWrite.IsEnabled = true;
this.btnRead.IsEnabled = false;
}
private async Task ReadFile()
{
// Get the local folder.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
if (local != null)
{
// Get the DataFolder folder.
var dataFolder = await local.GetFolderAsync("level");
// Get the file.
var file = await dataFolder.OpenStreamForReadAsync("level.txt");
// Read the data.
using (StreamReader streamReader = new StreamReader(file))
{
this.textBlock1.Text = streamReader.ReadToEnd();
}
}
}
}
}