Write To Text File Using Streamwriter C

/ Comments off
Write To Text File Using Streamwriter C 6,5/10 9284 votes

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:

  1. Rich Text File

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.

llkllk

8 Answers

Pass trueas the append parameter of the constructor:

SLaksSLaks

Change your constructor to pass true as the second argument.

sarveshsarvesh

You have to open as new StreamWriter(filename, true) so that it appends to the file instead of overwriting.

Jesus RamosJesus Ramos

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.

Sagar KuteSagar Kute
Umesh CHILAKAUmesh CHILAKA

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

TodTod

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.

user4064065
BrandonZeiderBrandonZeider

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?

WaypointWaypoint

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:

Øyvind BråthenØyvind Bråthen
Andrey TaptunovAndrey Taptunov

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:

JonJon
MarcoMarco

Replace this:

with this:

Using

true indicates that it appends text.

WaqasWaqas

Use this StreamWriter constructor with 2nd parameter - true.

Kirill PolishchukKirill Polishchuk

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. :)

ChrisChris

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.

Veroslav OlicVeroslav Olic

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

Vinod SrivastavVinod Srivastav

Replace this line:

with this code:

and then write your line to the text file like this:

ShahafShahaf

Not the answer you're looking for? Browse other questions tagged c#streamwriter or ask your own question.