4
votes

I have a RichTextBox and I want to save the text to a file. 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.

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++). txtClass is the RichTextBox.

private void btnToClipboard_Click(object sender, EventArgs e) { //Works as desired 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.

If I use SW.Write("Line One\r\nLineTwo\r\nLineThree") also works fine.

Thanks for your help

3
Did you try the SaveFile method located on the RichTextBox itself? msdn.microsoft.com/en-us/library/…Peter Lindholm
txtClass.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); Worked Perfectly. Thanks!Jonathan
@Jonathan- could you add Mr.Lindholms answer to this question and mark it as an answer? I've bumped into your question twice now since its still not answered :)Peter Lillevold
@Peter Lillevold - Of course!Jonathan

3 Answers

3
votes

Thanks to Peter Lindholm, who gave me the correct answer in a comment.

Did you try the SaveFile method located on the RichTextBox itself? http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.savefile(VS.71).aspx

1
votes

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.

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

So a simple solution is to manually replace \n with \r\n and works perfect as expected.

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);
    }
0
votes
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();
    }
}