7

This is the problem I wanted to solve:

  1. Create a file, add data and Save it in isolatedStorage.

  2. Open the file, add more data to this file.

How do I append data into this file? Will the new data line up ahead of the old data?

Some code will be appreciated.

Thanks

2
  • @ctacke: I have been reading and writing file. Just thought on UX side. user may want to keep writing data to the same file. here my code
    – MilkBottle
    Jun 3, 2011 at 15:18
  • StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(txtBlkDirName.Text + "\\" + strFilenm, FileMode.OpenOrCreate, isf)); writeFile.WriteLine(txtPreSetMsg.Text); writeFile.Close();
    – MilkBottle
    Jun 3, 2011 at 15:18

1 Answer 1

7

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

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.

Your options for appending would be:

  • 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.
1
  • Thank you for the solution. I will work on this direction. I wonder if I will be lucky next time :) Thanks.
    – MilkBottle
    Jun 3, 2011 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.