我想创建一个.txt文件并写入它,如果该文件已经存在,我只想添加更多行:
string path = @"E:\AppServ\Example.txt";
if (!File.Exists(path))
{
File.Create(path);
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The very first line!");
tw.Close();
}
else if (File.Exists(path))
{
TextWriter tw = new StreamWriter(path);
tw.WriteLine("The next line!");
tw.Close();
}
Run Code Online (Sandbox Code Playgroud)
但是第一行似乎总是被覆盖......我怎么能避免写在同一行(我在循环中使用它)?
我知道这是一件非常简单的事情,但我WriteLine之前从未使用过这种方法.我对C#完全不熟悉.
是否有正确的回车顺序然后新换行?对于文本编辑器,它们出现的顺序是否重要?
例如,而不是
\r\n
Run Code Online (Sandbox Code Playgroud)
这个
\n\r
Run Code Online (Sandbox Code Playgroud)
好像杰夫已经写了一篇关于这个主题的非常好的博客文章.