【问题标题】:StreamWriter: How to add a new line?StreamWriter:如何添加新行?
【发布时间】:2016-08-28 18:42:43
【问题描述】:

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

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));
            }

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

【问题讨论】:

  • 试试File.WriteAllText(fileName, textToWrite);

标签: c# file-io


【解决方案1】:

可能是平台问题?看Difference between "\n" and Environment.NewLine 试试 Environment.NewLine

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

【讨论】:

    【解决方案2】:

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

    例如:

    writer.WriteLine(this.currentRealWorldObj.Title);
    writer.WriteLine();
    writer.WriteLine();
    writer.WriteLine(this.currentRealWorldObj.Description);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      相关资源
      最近更新 更多