• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 37277
  • Last Modified:

C# - Append to a text file

Hi,

Lets say I have a text file.

Always when I try to write to it with the following code, It erases what was already in it:

                // create a writer and open the file
                TextWriter tw = new StreamWriter(MyTextfile.txt);

                // write a line of text to the file
                tw.WriteLine(msg);

                // close the stream
                tw.Close();

How can I write to a text file so I only add text to it (I don't delete the text that was already in the text file)?

Thank you.
0
Endelm
Asked:
Endelm
  • 2
1 Solution
 
gregoryyoungCommented:
TextWriter tw = new StreamWriter(MyTextfile.txt, true); http://msdn2.microsoft.com/en-us/library/36b035cb.aspx
0
 
gregoryyoungCommented:
0
 
EndelmAuthor Commented:
thank you.
0
 
feradzCommented:
Use constructor StreamWriter(string path, bool append);
example is given below
StreamWriter sw = new StreamWriter(@"C:\mydir\myfile.txt", true);
sw.WriteLine("Some lines");
sw.Close();

Open in new window

0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now