如何在C#中的上一行之后写一个文本文件?

ria*_*iad 0 c# io append

我的下面的代码创建了一个txt文件并将一些内容写入该文件.但是当我多次运行脚本时,我需要在前面的行之后写一个新行.码:

string filePath = "D:\\DOT_NET\\C#\\abc.txt";
FileInfo t = new FileInfo(filePath);
StreamWriter Tex = t.CreateText();
Tex.WriteLine("Hi freinds");
Tex.WriteLine("csharpfriends is the new url for c-sharp");
Tex.Write(Tex.NewLine);
Tex.Close();
Run Code Online (Sandbox Code Playgroud)

abc.txt文件的当前输出:

Hi friends
csharpfriends is the new url for c-sharp
Run Code Online (Sandbox Code Playgroud)

但是如果我多次运行脚本,我需要输出:

Hi friends
csharpfriends is the new url for c-sharp

Hi friends
csharpfriends is the new url for c-sharp

Hi friends
csharpfriends is the new url for c-sharp
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?请帮忙.

Gra*_*ote 6

StreamWriter有一个构造函数,允许您附加文本而不是只写入文件.构造函数是

new StreamWriter(string filepath, bool append)
Run Code Online (Sandbox Code Playgroud)

如果将bool设置为"true",则所有写入都将在文档的末尾.在你的例子中......

StreamWriter Tex = new StreamWriter(@"D:\DOT_NET\C#\abc.txt", true);
Run Code Online (Sandbox Code Playgroud)