SaveFile RichTextBox问题 [英] SaveFile RichTextBox Problem

查看:73
本文介绍了SaveFile RichTextBox问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RichTextBox和一个Button. 3

这是我的代码,用于保存编写的文本.

I have a RichTextBox and a Button. 3

This is my code to save the text that was written.

richTextBox1.SaveFile(@"c:/users/hp/desktop/Chat.txt");
richTextBox1.Clear();



我在richtextbox中编写内容,但是当我单击Button时,它并没有将我的新文本添加到以前的文本文件中.



I write in the richtextbox but when I click on the Button it doesn''t add my new text to previous text file.

Any Idea?

推荐答案

这里的每个人似乎都忽略了这一点,即这是 RichTextBox ! />
我假设您要保留可以输入到RichTextBox中的格式设置信息.

其他人也使它变得比所需的更加复杂.

基本上,您希望将信息附加到文件中.但是,您要编写RichText,而不是Text.

因此,RichTextBox提供了一个称为Rtf的属性,该属性是具有RTF格式的文本.

如果要获取原始文件并将其附加,只需使用File.AppendText打开流编写器,然后编写文本即可,就像这样:

Everyone here seems to be missing the point that this is a RichTextBox!

I am assuming that you want to preserve the formatting information that can be entered into the RichTextBox.

Others are also making it way more complex than it needs to be.

Basically, you want to append the information to the file. But, you want to write the RichText, not the Text.

So, the RichTextBox provides a property called Rtf that is the text with the rich text formatting.

If you want to take the original file and append it, just open a stream writer using File.AppendText and then write the text...like so:

using (StreamWriter writer = File.AppendText(@"c:\teste\chat.txt"))
{
  writer.Write(richTextBox1.Rtf);
}



就是这么简单...



it''s that simple...


richtextbox中没有重载.SaveFile用于文件追加...


使用以下代码解决您的问题

There is no Overload available in richtextbox.SaveFile for File appending...


Use the below code to slove your Problem

if (File.Exists("c:\\temp.txt"))
 {
     byte[] FileBuf = File.ReadAllBytes("c:\\temp.txt");
     byte[] RichBuf = Encoding.ASCII.GetBytes(richTextBox1.Text);
     byte[] Total = new byte[FileBuf.Length + RichBuf.Length];
     System.Buffer.BlockCopy(FileBuf, 0, Total, 0, FileBuf.Length);
     System.Buffer.BlockCopy(RichBuf, 0 , Total, FileBuf.Length , RichBuf.Length);
     File.WriteAllBytes("c:\\temp.txt", Total);
 }




试试这个

richTextBox1.SaveFile("c:\\users\\hp\\desktop\\Chat.txt", RichTextBoxStreamType.RichText)

杰瑞姆(Jerem)


try this

richTextBox1.SaveFile("c:\\users\\hp\\desktop\\Chat.txt", RichTextBoxStreamType.RichText)

jerem


这篇关于SaveFile RichTextBox问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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