Click to See Complete Forum and Search --> : Streamwriter Problem


vandel212
March 3rd, 2009, 01:32 PM
Hi, I'm try to write a program that has the user input different values into text boxes and then it puts those values into a text file when the user presses a button. My problem is that I don't want the program to overwrite the text that is already in the text file but add on to the end of it. So if the user presses the button a second time they will add more text to the document. Is there any way to have the streawriter start writing below the last line that has text?

Thanks,
Randell

RaleTheBlade
March 3rd, 2009, 08:33 PM
Pass a FileStream object to the StreamWriter and set its FileMode to FileMode.Append. This will create a new file if it doesnt exist, and also append text to the file.


StreamWriter writer = null

try
{
using (writer = new StreamWriter(new FileStream("myfile.txt", FileMode.Append)))
{
// Write to the stream writer
}
}
catch // Catch exceptions... Disk IO is pesky for exceptions
{
// Handle the exception
}
finally
{
if (writer != null)
{
writer.Close();
writer.Dispose();
}
}

dglienna
March 3rd, 2009, 11:11 PM
So if the user presses the button a second time they will add more

That's what APPEND does