我的下面的代码创建一个txt文件并将某些内容写入该文件。但是当我多次运行脚本时,我需要在前面的行之后写一行新行。代码:如何在C#中的前一行之后编写一个文本文件?
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();
在abc.txt
文件电流输出:
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
Hi friends
csharpfriends is the new url for c-sharp
我该怎么办那?请帮忙。
首先,我认为你应该把StreamWriter声明放在一个using块中。 – 2010-05-25 12:39:49