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?

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

[Alternate answer based on your updated question.]

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

See http://msdn.microsoft.com/en-us/library/ch779a3b.aspx[^]
 
Share this answer
 
Comments
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))
            {
                sw.WriteLine("This");
                sw.WriteLine("code");
                sw.WriteLine("will");
                sw.WriteLine("write");
                sw.WriteLine("text");
                sw.WriteLine("per");
                sw.WriteLine("line");
            }
        }
}


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)
   {
      sw.WriteLine(line);
   }
}
 
Share this answer
 
v2
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