开发者

how can i write line by line in txt data? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I try to write line by line data but. if i run my application. writng last text1 data in script.txt

private void button1_Click(object sender, EventArgs e)
{
   System.IO.TextWriter tw;
   tw = new StreamWriter("C:/Script.txt");
   tw.Writ开发者_开发问答eLine(textBox1.Text);
   tw.Close();
}


If you need to append line but not replace all contents then pass "true" as second parameter to constructor of StreamWriter:

tw = new StreamWriter("C:/Script.txt", true);


I think that for what you want to achieve (assuming you want to append each line to the end of the file), using File.AppenAllLines is the simplest way forward:

private void button1_Click(object sender, EventArgs e)
{
   File.AppendAllLines(@"C:\Script.txt", new[]{ textBox1.Text });
}

Alternatively, if you are not using .NET 4, you can use File.AppendAllText instead, adding a line feed to the end:

File.AppendAllText(@"C:\Script.txt", textBox1.Text + Environment.NewLine);


OR

using (StreamWriter sw = File.AppendText(fileName))
{
     sw.WriteLine(line);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜