将新行附加到文件的最简洁/最短的方法

Jam*_*are 6 .net c# windows

有各种方法将文本附加到文件,虽然我想知道是否知道最短的方法来做到这一点

例如,在现有的txt文件中添加一个新行:

line1
line2
<new-line-here>
Run Code Online (Sandbox Code Playgroud)

dri*_*iis 11

这将是这样的:

File.AppendAllText("c:\filepath.txt", "text to append");
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参见File.AppendAllText.该文件类有很多做普通的文件操作有用的静态方法.

  • @driis:追加所有文字不一定要添加新行. (4认同)
  • 在命名空间中,System.IO. (3认同)

M.B*_*ock 7

File类的静态方法使其非常简单:

File.AppendAllLines("filename.txt", new string[] { "text to append" });
Run Code Online (Sandbox Code Playgroud)

编辑:使用数组略短.