且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据附加到 Windows Phone 的独立存储中的同一个文件中

更新时间:2023-10-10 19:34:46

您的代码如下所示(来自您在问题中的评论 - 下次编辑问题并插入代码,使其更具可读性,我们不必转发):

Your code looks like this (from your comment in the question - next time edit the question and insert the code so it's more readable and we don't have to repost it):

StreamWriter writeFile = new StreamWriter(
    new IsolatedStorageFileStream(
        txtBlkDirName.Text + "\\" + strFilenm, 
        FileMode.OpenOrCreate, 
        isf)); 
writeFile.WriteLine(txtPreSetMsg.Text); writeFile.Close(); 

请注意,您使用的模式是 OpenOrCreate,它将打开现有文件并将您的流指针放在开头.如果您立即开始写入,它将覆盖文件中的任何内容.

Note that the mode you use is OpenOrCreate, which is going to open the existing file and put your stream pointer at the start. If you immediately start writing, it's going to overwrite anything in the file.

您的附加选项是:

  • 改用FileMode.Append,这样在打开后指针就已经在流的末尾了
  • 保留现有内容,但在编写之前,调用Seek(0, SeekOrigin.End) 手动将指针移至文件末尾.
  • Use FileMode.Append instead so the pointer is already at the end of the stream after opening
  • Keep what you have but before writing, call Seek(0, SeekOrigin.End) to move the pointer to the end of the file manually.