27

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:

TextWriter tsw = new StreamWriter(@"C:\Hello.txt");

//Writing text to the file.
tsw.WriteLine("Hello");

//Close the file.
tsw.Close();

I want it to write Hello every time I run the program, not overwrite the previous text file. Thanks for reading this.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
llk
  • 2,501
  • 7
  • 36
  • 43
  • What is @ meaning here ? thanks – user2420472 Jan 31 '14 at 16:46
  • @user2420472 "[The `@` special character serves as a verbatim identifier.](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim)" In this case, it means the backslash will not be used as an escape character, but as a verbatim backslash. – Bondolin Aug 17 '21 at 19:22

9 Answers9

63

Pass true as the append parameter of the constructor:

TextWriter tsw = new StreamWriter(@"C:\Hello.txt", true);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
10

Change your constructor to pass true as the second argument.

TextWriter tsw = new StreamWriter(@"C:\Hello.txt", true);
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
sarvesh
  • 2,743
  • 1
  • 20
  • 30
5

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

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
4

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.

string strLogText = "Some details you want to log.";

// Create a writer and open the file:
StreamWriter log;

if (!File.Exists("logfile.txt"))
{
  log = new StreamWriter("logfile.txt");
}
else
{
  log = File.AppendText("logfile.txt");
}

// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strLogText);
log.WriteLine();

// Close the stream:
log.Close();
Sagar Kute
  • 171
  • 1
  • 3
  • 10
3

Best thing is

File.AppendAllText("c:\\file.txt","Your Text");
qJake
  • 16,821
  • 17
  • 83
  • 135
Umesh CHILAKA
  • 1,466
  • 14
  • 25
1

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.


string FILE_NAME = "Log" + System.DateTime.Now.Ticks.ToString() + "." + "txt"; 
string str_Path = HostingEnvironment.ApplicationPhysicalPath + ("Log") + "\\" +FILE_NAME;


 if (!File.Exists(str_Path))
 {
     File.Create(str_Path).Close();
    File.AppendAllText(str_Path, jsonStream + Environment.NewLine);

 }
 else if (File.Exists(str_Path))
 {

     File.AppendAllText(str_Path, jsonStream + Environment.NewLine);

 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
1

Look into the File class.

You can create a streamwriter with

StreamWriter sw = File.Create(....) 

You can open an existing file with

File.Open(...)

You can append text easily with

File.AppendAllText(...);
Tod
  • 8,192
  • 5
  • 52
  • 93
0
using (StreamWriter writer = File.AppendText(LoggingPath))
{
    writer.WriteLine("Text");
}
BrandonZeider
  • 8,014
  • 2
  • 23
  • 20
0

none of the above did not work I found the solution myself

 using (StreamWriter wri = File.AppendText("clients.txt"))
        {
            wri.WriteLine(eponimia_txt.Text + "," + epaggelma_txt.Text + "," + doy_txt.Text + "," + dieuthini_txt.Text + ","
                + proorismos_txt.Text + "," + poly_txt.Text + "," + sxePara_txt.Text + "," + afm_txt.Text + ","
                + toposFortosis_txt.Text + ",");
        }