0

我一直在关注本教程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();
            }

        }
    }
}

}

4

1 回答 1

0

我相信您应该使用以下选项打开您的level.txt文件,OpenIfExists而不是ReplaceExisting

// Create a new file named DataFile.txt.
var file = await dataFolder.CreateFileAsync( "level.txt", CreationCollisionOption.OpenIfExists );
于 2013-09-26T13:57:03.167 回答