使用C#保存/加载RichTextBox内容以及其他数据 [英] Saving/Loading RichTextBox Content with other data using C#

查看:124
本文介绍了使用C#保存/加载RichTextBox内容以及其他数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试弄清楚如何将RichTextbox中的内容保存到流中(当前使用FileStream)并与其他数据一起进行.然后,当然我希望能够从此文件加载.我目前正在尝试按照以下方式使用某些东西.

I am currently trying to figure out how to save the content from a RichTextbox into a stream(currently using FileStream) and do this alongside a bunch of other data. Then of course I want to be able to load from this file. I was currently trying to use something along the following lines.

FileStream stream = new FileStream(); //this is actually correctly defined.
ASCIIEncoding encoding = new ASCIIEncoding();

//write Title
byte[] array = encoding.GetBytes(Title);
stream.WriteByte(Convert.ToByte(array.Length));
stream.Write(array, 0, array.Length);

//save textRange
textRange.Save(stream, System.Windows.DataFormats.Rtf);

//write Subtitle
byte[] array = encoding.GetBytes(Subtitle);
stream.WriteByte(Convert.ToByte(array.Length));
stream.Write(array, 0, array.Length);
//ect...and something very similar for Loading a file.

这基本上就是我想要做的.我实际上是在保存2个TextRanges和更多的属性.所以我的问题是 TextRange.Load() 读取到文件的末尾......考虑到我需要保存/加载 2 个 TextRanges,我无法使用它.因此,在这里我试图提出另一种方法来保存/加载带有其他数据的RichTextBox的内容.我不必使用流.我对任何可行的解决方案都持开放态度.提前致谢!

This is basically what I am trying to do. I am actually saving 2 TextRanges and a bunch more Properties. So my problem is that TextRange.Load() reads to the end of the file...making it impossible for me to use that considering I have 2 TextRanges I need to save/load. So here I am trying to come up with another way to be able to save/load the content of a RichTextBox with other data. I dont have to use a Stream. I am pretty much open to any feasible solutions. Thanks in advance!

〜贾森(Jasson)

~Jasson

推荐答案

您可以加载/保存到MemoryStream以解决读取文件末尾的问题.看起来可能像这样:

You could load/save to a MemoryStream to solve your issue with reading to the end of the file. It could look like this:

  • 将文件加载到内存中
  • 将该文件的一部分(即richtextbox的内容)加载到MemoryStream中
  • 从该MemoryStream加载richtextbox内容

还是您想知道如何创建和解析文件以包含标题,内容和任何其他字段的不同部分?

Or are you wanting to know how you'd create and parse a file to contain different sections for the title, and the contents, and any other fields ?

这篇关于使用C#保存/加载RichTextBox内容以及其他数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆