Solved

C# - Append to a text file

Posted on 2007-11-15
4
37,207 Views
Last Modified: 2013-11-07
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
Comment
Question by:Endelm
  • 2
4 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 20291428
TextWriter tw = new StreamWriter(MyTextfile.txt, true); http://msdn2.microsoft.com/en-us/library/36b035cb.aspx
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 20291443
0
 

Author Closing Comment

by:Endelm
ID: 31409399
thank you.
0
 

Expert Comment

by:feradz
ID: 20760757
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now