覆盖 wp8 中的现有文件 [英] Overwriting the existing file in wp8

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

问题描述

我正在制作一个应用程序,我想在其中覆盖我的文件,就像我在文件中写入一个数字并读取但下次我想将新数字添加到现有文件然后读取它

i am making an app in which i want to overwrite my file , like i am writing a number in the file and reading but the next time i want to add the new number to the existing file then read it

为了写入文件,我正在使用代码

for writing into the file i am using the code

private async void cross_Click(object sender, RoutedEventArgs e)
{


    byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.ScoreTB.Text.ToCharArray());

    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    var dataFolder = await local.CreateFolderAsync("MyFolder", CreationCollisionOption.OpenIfExists);

    var file = await dataFolder.CreateFileAsync("MyFile.txt", CreationCollisionOption.ReplaceExisting);

    using (var s = await file.OpenStreamForWriteAsync())
    {
        s.Write(fileBytes, 0, fileBytes.Length);
    }
}

和读取文件

private async void aboutus_Click(object sender, RoutedEventArgs e)
{
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    if (local != null)
    {
        // Get the DataFolder folder.
        var dataFolder = await local.GetFolderAsync("MyFolder");

        // Get the file.
        var file = await dataFolder.OpenStreamForReadAsync("MyFile.txt");

        // Read the data.
        using (StreamReader streamReader = new StreamReader(file))
        {
            this.Score.Text = streamReader.ReadToEnd();
        }
    }
    else
    {
        MessageBox.Show("No File Created");
    }
}

请帮助我如何在 Windows 手机应用程序开发中覆盖现有文件.

just help me how to overwrite a existing file in windows phone app development.

提前致谢

推荐答案

据我所知,您不需要覆盖,而是需要追加.

As I understand, you don't need an overwrite, but append.

1) 将 ReplaceExisting 改为 OpenIfExists:

  var file = await dataFolder.CreateFileAsync("MyFile.txt", CreationCollisionOption.OpenIfExists );

2) 在写入前寻找流的末尾:

2) seek to the end of the stream before write:

  using (var s = await file.OpenStreamForWriteAsync())
  {
    s.Seek(0, SeekOrigin.End );
    s.Write(fileBytes, 0, fileBytes.Length);
  }

这篇关于覆盖 wp8 中的现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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