\n未添加到文本文件中

Yea*_*iam 0 c# io file text-files

我想在C#中写入一个文本文件.我的意见如下

string input = "46 47\n 48 49";
Run Code Online (Sandbox Code Playgroud)

我希望这写成如下

46 47

48 49

但它写得像

46 47 48 49

我使用以下代码

string input = "46 47\n 48 49";
string path = @"d:\temp\MyTest.txt";

File.WriteAllText(path, input);
Run Code Online (Sandbox Code Playgroud)

Hab*_*bib 5

使用\r\nEnvironment.NewLine喜欢:

string input = "46 47\r\n48 49";
Run Code Online (Sandbox Code Playgroud)

要么

string input = "46 47" + Environment.NewLine + "48 49";
Run Code Online (Sandbox Code Playgroud)

编辑:就修改现有字符串值而言,您可以这样做String.Replace:

string newValue = input.Replace("\n",Environment.NewLine);
Run Code Online (Sandbox Code Playgroud)

然后用newValueFile.WriteAllText