|
-
March 3rd, 2009, 02:32 PM
#1
Streamwriter Problem
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
-
March 3rd, 2009, 09:33 PM
#2
Re: Streamwriter Problem
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.
Code:
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();
}
}
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
March 4th, 2009, 12:11 AM
#3
Re: Streamwriter Problem
So if the user presses the button a second time they will add more
That's what APPEND does
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|