Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append data into the same file in IsolatedStorage for Windows Phone

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

like image 636
MilkBottle Avatar asked Jun 03 '11 13:06

MilkBottle


1 Answers

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.
like image 160
ctacke Avatar answered Oct 19 '22 12:10

ctacke