Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
I have a rich text box of which the content should be written to a file, but line per line, just like its displayed in the rich text box.
how do I do this?

i want to retain the formatting, that's very important.
Updated 23-Apr-11 4:25am

[Alternate answer based on your updated question.]

If you want to save it as RTF, use an overload of RichTextBox.SaveFile.

Share this answer
Red&Black 23-Apr-11 10:41am    
thanks allot, this is just what i wanted.
Nish Nishant 23-Apr-11 10:46am    
You are welcome!
The following code snippet helps you to write a text per a single line.
Note I am assuming that you want to write on a text file.

public static void Main()
        string path = @"c:\test.txt";
        if (!File.Exists(path))
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))

So you can able to write each line each of RichTextBox control as follows

string path = @"c:\test.rtf";
using (StreamWriter sw = File.CreateText(path))
   foreach (string line in  richTextBox1.Lines)
Share this answer
You can use either File.WriteAllLines or just File.WriteAllText. Whichever works better for you.
Share this answer
This is not quite clear.

1. What is the file type?
2. Is there any kind of formatting that you want to retain?

If you just want to write the text with no formatting, try File.WriteAllLines or use Stream.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900