Solved

C# - Append to a text file

Posted on 2007-11-15
4
37,231 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

789 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