简体   繁体   English

使用StreamWriter不起作用\\ n(C#)

[英]With StreamWriter doesn't work \n (C#)

I have a problem with the C# Stream Writer. 我的C#Stream Writer有问题。 I use the following Code: 我使用以下代码:

//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 \\ra █(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. 谢谢你的帮助。

如果要手动执行,请使用Environment.NewLine作为行分隔符或"\\r\\n"

Line Separator(newLine) is \\r\\n not \\n\\r , 行分隔符(newLine)是\\r\\n而不是\\n\\r

change your text as : 将您的文字更改为:

       string text = "l1\r\nl2\r\nl3\r\nl5";

Try string text = @"l1\\n\\rl2\\n\\rl3\\n\\nl5"; 尝试string text = @"l1\\n\\rl2\\n\\rl3\\n\\nl5"; . To prevent character stuffing. 防止字符填充。

This is binary format: 这是二进制格式:

writer.Write(text); 

This is line sequential format: 这是线序格式:

writer.WriteLine(text); 

You have to use WriteLine format... 你必须使用WriteLine格式......

您可以像这样使用Environment.NewLine

streamWriter.Write(String.Concat(Enumerable.Repeat(Environment.NewLine, n).ToArray()));

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM