I am trying to write to a file using FileStream and want to write a second line and then write the first line. I use Seek () to go back to the top after writing the second line, and then write the first line. It replaces the second line (or part of it, depending on the length of the first line). How can I not replace the second line?
var fs = new FileStream("my.txt", FileMode.Create); byte[] stringToWrite = Encoding.UTF8.GetBytes("string that should be in the end"); byte[] stringToWrite2 = Encoding.UTF8.GetBytes("first string\n"); fs.Write(stringToWrite, 0, stringToWrite.Length); fs.Seek(0, SeekOrigin.Begin); fs.Write(stringToWrite2, 0, stringToWrite2.Length);
The following is written to the file:
first string hould be in the end
I want him to be
first string string that should be in the end
thanks
source share