StreamWriter:如何添加新行?

Ske*_*lio 1 c# file-io

我想将字符串写入文件。我的问题是我无法在文件中添加新行。

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(String.Format("{0}.txt", svDialog.FileName)))
            {
                writer.Write(String.Format("{0}\n\n{1}", this.currentRealWorldObj.Title, this.currentRealWorldObj.Description));
            }
Run Code Online (Sandbox Code Playgroud)

如您所见,我在 String.Format 方法中添加了 2 个新行(“\n\n”)。但是当我打开这些行时没有添加。如何添加它们?

Jam*_*ood 8

为此有一个特定的WriteLine函数。

例如:

writer.WriteLine(this.currentRealWorldObj.Title);
writer.WriteLine();
writer.WriteLine();
writer.WriteLine(this.currentRealWorldObj.Description);
Run Code Online (Sandbox Code Playgroud)


Edw*_*ard 6

可能是平台问题?看看"\n" 和 Environment.NewLine 的区别 试试 Environment.NewLine

writer.Write(String.Format("{0}{2}{2}{1}", this.currentRealWorldObj.Title, this.currentRealWorldObj.Description,Environment.NewLine));
Run Code Online (Sandbox Code Playgroud)