编写和从文件中读取 [英] Write and Read from a file

查看:143
本文介绍了编写和从文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这个教程的 http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698%28v=vs.105%29.aspx

I have been following this tutorial http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681698%28v=vs.105%29.aspx.

到目前为止,是我在寻找,但唯一的问题是,当我关闭并重新打开应用程序文件和文本不再被保存,因此我要将该文件与文本永远保存。

So far is what I was searching for, but the only problem is that when i close and open the app again the file and the text is not saved anymore, so I want to the file be saved forever with the text.

我想将它保存在这里的 http://gyazo.com/82e838cd2385cea7021647a8d39f49a8.png/level/batlevel.txt 。所以,当我可以打开应用程序再次,这是写有它会在那里文字

I want to it be saved here http://gyazo.com/82e838cd2385cea7021647a8d39f49a8.png/level/batlevel.txt. So when I can open the app again the text that was write there it will be there

    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();
            }

        }
    }
}

}

推荐答案

我相信你应该会打开你的 level.txt 文件中的 OpenIfExists 选项,而不是 ReplaceExisting

I believe you should be opening your level.txt file with the OpenIfExists option instead of ReplaceExisting :

// Create a new file named DataFile.txt.
var file = await dataFolder.CreateFileAsync( "level.txt", CreationCollisionOption.OpenIfExists );

这篇关于编写和从文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆