Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody,

I have a richtextbox and I want to save its document into a .txt file. I used this code to save content
C#
TextRange t = new TextRange(this.MyRichTextBox.Document.ContentStart, this.MyRichtextbox.document.contentend);
FileStream fs = new FileStream (MyPath, FileMode.Open, FileAccess.Write);
t.Save(fs, DataFormats.Text)
Fs.close()


And this to load

XML
TextRange t = new TextRange(this.MyRichTextBox.Document.ContentStart, this.MyRichtextbox.document.contentend);
FileStream fs = new FileStream (MyPath, FileMode.Open, FileAccess.Read);
t.Load(fs, DataFormats.Text)
Fs.close()


The problem is that tabulations are not saved correctly. What should I do if I want to mantain, as filetype, Text? I decided it because rtf has formatting property which I do not want my program to use inside the richtextbox...

Thanks!!
Posted

You will never can reliably mimic RTF (or any other as or more advanced) formatting with tabs, blank spaces and similar stuff of a text file. Why doing that at all? This is just a bad idea. You can use the same RTF or, say, HTML. These two formats can be viewed by everyone, on nearly all platforms. Another possibility is PDF.

Alternatively, change the textual representation to something really simple, something which a plain text can do, without columns or other formatting inappropriate to text files.

—SA
 
Share this answer
 
Comments
LLLLGGGG 17-Jun-14 5:32am    
Hi thanks for your reply, but I succeded in solving the problem. Unfortunately, (don't ask me why, because I don't know) DataFormats class has many different formats, including Html, but richtextbox cam use only rtf, text, xaml and xamlpackage. I tried with rtf, but rtf formatting was prior over my richtextbox formatting, xaml had problems with closed tags... The only format which worked perfectly, according to my needs, is XamlPackage, so I have chosen it.
Hi,

I succeded in solving my problem:
As rtf formatting is prior over the richtextbox one, I couldn't use it (at least as I could have noticed).
I solved my problem using XamlPackage which gave me no problems at all while xaml gave me strange problems with

So the solution I've used is


I used this code to save content

C#
TextRange t = new TextRange(this.MyRichTextBox.Document.ContentStart, this.MyRichtextbox.document.contentend);
FileStream fs = new FileStream (MyPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
t.Save(fs, DataFormats.XamlPackage)
Fs.close()

 
And this to load
 
C#
TextRange t = new TextRange(this.MyRichTextBox.Document.ContentStart, this.MyRichtextbox.document.contentend);
FileStream fs = new FileStream (MyPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
t.Load(fs, DataFormats.XamlPackage)
Fs.close()
 
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