14,825,880 members
1.00/5 (1 vote)
See more:
well in to write text box text into text file. i don this but it over ride my previous line data
how can i write new data on new line ?

What I have tried:

C#
using (StreamWriter sw1 = new StreamWriter(@"D:\text.txt"))
{
sw1.WriteLine(textBox1.Text);
sw1.Close();
}
Posted
Updated 8-May-16 20:58pm
v2
Comments
Philippe Mori 8-May-16 16:03pm

Use code block for code. Don't be too lazy.

## Solution 3

Since version 2.0 of the .NET Framework, writing text to a file is made very easy:

The System.IO facility provides two useful methods:

File.WriteAllText Method [^]

File.WriteAllLines Method [^]

There are ready-to-use code examples at both those links.

These methods have the advantage that, unlike, using the other techniques shown here, they do not append any extra characters to the text saved to the file.

Comments:

1. any time you save whatever to a stream, or file: verify the path you will use is valid, usable, etc., before attempting to write/save.

Consider: if the file already exists if you wish to over-write it.

2. if the user is going to be selecting the save/write location at run-time: take the time to study the File and Folder Selection Dialogs, and how to configure them so the user can be guided to an appropriate choice.

3. keep in mind that Text is encoded, and may be culture-dependent. plan for that.

## Solution 1

use File.AppendText[^] , well there are many ways to do. this is one of the way.

C#
using (StreamWriter sw1 = File.AppendText(@"D:\text.txt"))
{
sw1.WriteLine(textBox1.Text);
// sw1.Close(); // not necessary, since you are using 'using', it will close while disposing
}
v3
Comments

Right, one of the ways; a 5.
—SA
Karthik_Mahalingam 8-May-16 22:40pm

Thanks Sergey

## Solution 2

You can add option append or not append for StreamWriter.
C#
using (StreamWriter sw1 = new StreamWriter(@"D:\text.txt", true))
{
sw1.WriteLine(textBox1.Text);
sw1.Close();
}

true: add new item on new line.
false: overwrite current file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 90 OriginalGriff 80 Greg Utas 55 Rick York 40 Dave Kreskowiak 40
 OriginalGriff 1,666 Richard MacCutchan 765 CPallini 535 Dave Kreskowiak 480 CHill60 435

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900