简体   繁体   English

RichTextBox获取文字文本

[英]RichTextBox getting literal text

I'm trying to save the text from my RichTextBox to a text file. 我正在尝试将文本从我的RichTextBox保存到文本文件。 However, upon doing so, the new lines in the RTB aren't considered "new lines" and hence a \\n isn't appended in the text file, so if I have: 但是,执行此操作时,RTB中的新行不被视为“新行”,因此\\n未附加到文本文件中,因此如果我有:

This is a test
line of content

It will write to the text file as: 它将写入文本文件:

This is a testline of content

I'm saving the text the following way: 我按以下方式保存文本:

File.WriteAllText(currentFile, richTextBox1.Text);

I'm just wondering if there's any solution for this. 我只是想知道是否有任何解决方案。 Thanks. 谢谢。

Try this: 尝试这个:

StreamWriter sw = File.CreateText(currentFile);
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
    sw.WriteLine(richTextBox1.Lines[i]);
}
sw.Flush();
sw.Close();

or use the rtb.SaveFile() method: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile%28VS.71%29.aspx 或使用rtb.SaveFile()方法: http//msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile%28VS.71%29.aspx

请改用构建的SaveFile方法。

richTextBox1.SaveFile(currentFile, RichTextBoxStreamType.PlainText);

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

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