Write To Text File Using Streamwriter C
I can't seem to figure out how to write data to a file without overwriting it. I know I can use File.appendtext but I am not sure how to plug that into my syntax. Here is my code:
I want it to write Hello every time I run the program, not overwrite the previous text file. Thanks for reading this.
Use StreamWriter from System.IO to write text files. Place StreamWriter in using statements. VVYX(XXr writer = VVVXYXXrY('C: log.txt', true)) VV{ VVVwriter. Initializes a new instance of the StreamWriter class for the specified file by using the specified encoding and default buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.
8 Answers
Pass true
as the append
parameter of the constructor:
Change your constructor to pass true as the second argument.
You have to open as new StreamWriter(filename, true)
so that it appends to the file instead of overwriting.
Here's a chunk of code that will write values to a log file. If the file doesn't exist, it creates it, otherwise it just appends to the existing file. You need to add 'using System.IO;' at the top of your code, if it's not already there.
Rich Text File
Look into the File class.
You can create a streamwriter with
You can open an existing file with
You can append text easily with
First of all check if the filename already exists, If yes then create a file and close it at the same time then append your text using AppendAllText
. For more info check the code below.
Not the answer you're looking for? Browse other questions tagged c#textoverwrite or ask your own question.
I want to append lines to my file. I am using a StreamWriter:
The output of my file should be several strings below each other, but I have only one row, which is overwritten every time I run this code.
Is there some way to let the StreamWriter append to an existing file?
10 Answers
Use this instead:
With this overload of the StreamWriter constructor you choose if you append the file, or overwrite it.
C# 4 and above offers the following syntax, which some find more readable:
I assume you are executing all of the above code each time you write something to the file. Each time the stream for the file is opened, its seek pointer is positioned at the beginning so all writes end up overwriting what was there before.
You can solve the problem in two ways: either with the convenient
or by explicitly repositioning the stream pointer yourself:
Replace this:
with this:
true
indicates that it appends text.
Use this StreamWriter
constructor with 2nd parameter - true
.
Another option is using System.IO.File.AppendText
This is equivalent to the StreamWriter overloads others have given.
Also File.AppendAllText may give a slightly easier interface without having to worry about opening and closing the stream. Though you may need to then worry about putting in your own linebreaks. :)
Actually only Jon's answer (Sep 5 '11 at 9:37) with BaseStream.Seek worked for my case. Thanks Jon! I needed to append lines to a zip archived txt file.
One more simple way is using the File.AppendText
it appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist and returns a System.IO.StreamWriter
Replace this line:
with this code:
and then write your line to the text file like this: