C# how to write multiple lines in a text file?

左心房为你撑大大i 提交于 2019-12-06 22:26:26

Open the file for append.

FileStream xuids = new FileStream(xuidspath, FileMode.Append);

just use StringBuilder class and File.WriteXXX methods.

StringBuilder sb = new StringBuilder();
sb.AppendLine(textBox.Text + " " + textbox2.Text);

File.WriteAllText("c:\xuids.txt",sb.ToString();

Use an overload:

public StreamWriter(string path, bool append);

i.e

TextWriter xuids = new StreamWriter(xuidspath,true);

Change:

TextWriter xuids = new StreamWriter(xuidspath);

To:

TextWriter xuids = new StreamWriter(xuidspath, true);

The second parameter is append. From MSDN (http://msdn.microsoft.com/en-us/library/36b035cb.aspx) :

Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.

Aref Khandan
RichTextBox rch = new RichTextBox();
rch.Text = cmn;
foreach (string l in rch.Lines)
    strw.WriteLine(l);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!