我是 C# 文件处理的新手,我正在制作一个非常简单的程序。代码如下:
class MainClass 
{
    public static void Main()
    {
        var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
        sw.Write("HelloWorld" +Environment.NewLine);
        sw.Write("ByeWorld");
        sw.Close(); 
        Console.ReadLine();
    }
}
上面的代码在文本文件中产生以下预期结果:
HelloWorld
ByeWorld
我还写了一些修改后的代码版本,如下所示:
class MainClass 
{
    public static void Main()
    {
        var sw = new StreamWriter("C:\\Users\\Punit\\Desktop\\hello.txt");
        sw.Write("HelloWorld\n");
        sw.Write("ByeWorld");
        sw.Close(); 
        Console.ReadLine();
    }
}
在这里而不是使用
Environment.Newline
我直接将“\n”添加到“HelloWorld”行。这产生了以下输出(在文本文件中):
HelloWorldByeWorld
我的问题是为什么第二段代码不起作用?(不在文本文件中生成换行符)