Main Site Documentation

SD card write line?


I can add a line of text to a file written on a sd card.
I managed to create (name hello.txt) and write 3 lines, but if I want to add another one without rewriting the first three as I do?
thanks for the help

StreamWriter textFile = new StreamWriter(rootDirectory + @ "\hello.txt");
             textFile.WriteLine("Hello SD card");
             textFile.WriteLine("Hello M.");
             textFile.WriteLine("3 riga!!!!!!!!!!");


@ scorpion1 - The constructor for StreamWriter has an interesting parameter which may help.


Oops I started writing a reply then clicked the wrong button.

What I was going to say was: You are overwriting the data because when you open a stream it opens at the start of the file. Setting the append parameter to true as mentioned earlier will open at the end so you can append data to the end of the file.

Alternatively you can access the stream contained in the filestream like this:

textfile.BaseStream.Seek(0, SeekOrigin.End); 

The seek function will let you move a number of bytes from a specific origin. So in this case we are saying move 0 bytes from the end of the file which will cause what you write from then on to add to the end.