Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 23-Apr-11 5:08am
Edited 23-Apr-11 5:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

[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[^]
  Permalink  
Comments
Red&Black at 23-Apr-11 10:41am
   
thanks allot, this is just what i wanted.
Nishant Sivakumar at 23-Apr-11 10:46am
   
You are welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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);
        }
     }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use either File.WriteAllLines or just File.WriteAllText. Whichever works better for you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 BillWoodruff 108
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 23 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100