Solved

C# - Append to a text file

Posted on 2007-11-15
4
37,248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

717 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