使用 StreamWriter 不起作用 \n (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13347104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With StreamWriter doesn't work \n (C#)
提问by Waronius
I have a problem with the C# Stream Writer. I use the following Code:
我的 C# Stream Writer 有问题。我使用以下代码:
//Constructor
public EditorTXTFile
{
FileStream f = File.Create(System.IO.Directory.GetCurrentDirectory() + "\Output.txt");
f.Close();
}
//Function AddText
public void AddLogFileText(string text)
{
string text = "l1\n\rl2\n\rl3\n\nl5";
StreamWriter writer = new StreamWriter(System.IO.Directory.GetCurrentDirectory() + "\Output.txt", true);
writer.Write(text);
writer.Close();
}
When I open Output.txt it shows for \n or \r a █(which means not showable symbol) and the whole string is in one line... Later should the text hand over the function, so I can't write the text with .WriteLine because I don't know if the actual string is on the same line or in a new line.
当我打开 Output.txt 时,它显示为 \n 或 \ra █(这意味着不可显示的符号)并且整个字符串在一行中......后来文本应该移交函数,所以我不能写文本使用 .WriteLine 因为我不知道实际的字符串是在同一行还是在新行中。
What make I wrong?
是什么让我错了?
Thanks for any help.
谢谢你的帮助。
采纳答案by Alexei Levenkov
Use Environment.NewLineas line separator or "\r\n"if you want to do it by hand.
使用Environment.NewLine作为行分隔符或者"\r\n",如果你想通过做手工。
回答by Michael Sallmen
Try string text = @"l1\n\rl2\n\rl3\n\nl5";. To prevent character stuffing.
试试string text = @"l1\n\rl2\n\rl3\n\nl5";。防止字符填充。
回答by Yogendra Singh
Line Separator(newLine) is \r\nnot \n\r,
行分隔符(newLine)\r\n不是\n\r,
change your text as :
将您的文字更改为:
string text = "l1\r\nl2\r\nl3\r\nl5";
回答by Jorge Lola
This is binary format:
这是二进制格式:
writer.Write(text);
This is line sequential format:
这是行顺序格式:
writer.WriteLine(text);
You have to use WriteLineformat...
你必须使用WriteLine格式...
回答by Andrei Marincas
You can use Environment.NewLinelike this:
你可以这样使用Environment.NewLine:
streamWriter.Write(String.Concat(Enumerable.Repeat(Environment.NewLine, n).ToArray()));
回答by Aref Khandan
i tried to write a class and seprate "\n"s but i found rich text box!!
我试图编写一个类并分离“\n”,但我发现了富文本框!!
yeah! it works:
是的!有用:
RichTextBox rch = new RichTextBox();
rch.Text = cmn;
foreach (string l in rch.Lines)
strw.WriteLine(l);

