[英]Writing the content of a RichTextBox to a file
I have a RichTextBox and I want to save the text to a file. 我有一个RichTextBox,我想将文本保存到文件中。 Each line of the RichTextBox are ended with CR+LF ("\\n\\r") but when i save it to a file, the lines only contains the LF char at the end. RichTextBox的每一行都以CR + LF(“\\ n \\ r”)结束,但是当我将它保存到文件时,行末尾只包含LF char。
If I copy the content to the clipboard instead of a file all goes right (The content of the clipboar has CR+LF at the end of each line, I can see it when I paste in Notepad++). 如果我将内容复制到剪贴板而不是文件都是正确的(剪贴板的内容在每行的末尾都有CR + LF,我可以在粘贴Notepad ++时看到它)。 txtClass is the RichTextBox. txtClass是RichTextBox。
private void btnToClipboard_Click(object sender, EventArgs e) { //Works as desired Clipboard.SetText(txtClass.Text); private void btnToClipboard_Click(object sender,EventArgs e){//按需工作Clipboard.SetText(txtClass.Text); } }
private void btnToFile_Click(object sender, EventArgs e)
{
//Don't work as desired
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter SW = new System.IO.StreamWriter(saveFileDialog1.FileName, false, Encoding.ASCII);
SW.Write(txtClass.Text);
SW.Close();
}
}
At this moment, I also tried with 此刻,我也试过了
SW.NewLine = "\r\n";
SW.Newline = Environment.NewLine
and with all Enconding avalilables. 以及所有Enconding avalilables。
If I use SW.Write("Line One\\r\\nLineTwo\\r\\nLineThree") also works fine. 如果我使用SW.Write(“Line One \\ r \\ nLineTwo \\ r \\ nLineThree”)也可以正常工作。
Thanks for your help 谢谢你的帮助
Thanks to Peter Lindholm, who gave me the correct answer in a comment. 感谢Peter Lindholm,他在评论中给了我正确的答案。
Did you try the SaveFile method located on the RichTextBox itself? 您是否尝试过RichTextBox本身的SaveFile方法? http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile(VS.71).aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile(VS.71).aspx
I had the same issue - but didn't want to save as a RichTextBox - just a standard simple txt file that could easily be read and edited in notepad. 我有同样的问题 - 但不想保存为RichTextBox - 只是一个标准的简单txt文件,可以在记事本中轻松读取和编辑。
The issue is for some reason the StreamWriter class does NOT write the \\n into the file (still not sure why - you would think it would) :) 问题是由于某种原因,StreamWriter类不会将\\ n写入文件(仍然不确定为什么 - 你会认为它会):)
So a simple solution is to manually replace \\n with \\r\\n and works perfect as expected. 所以一个简单的解决方案是用\\ r \\ n手动替换\\ n,并按预期完美地工作。
See code snipit below: 请参阅下面的代码片段:
if ((myStream = ScriptFileSaveDB.OpenFile()) != null)
{
using (StreamWriter sr = new StreamWriter(myStream))
{
//Since \n (newlines) are not being written correctly as \r\n
//Go thru Text and replace all "\n" with \r\n
tempStr = ScriptProgramWindowRTB.Text;
tempStr = tempStr.Replace("\n", "\r\n");
sr.Write(tempStr);
}
private void btnToFile_Click(object sender, EventArgs e)
{
//Don't work as desired
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter SW = new System.IO.StreamWriter(
saveFileDialog1.FileName, false, Encoding.ASCII);
SW.Write(txtClass.Text);
SW.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.