我有一个 Winforms 项目,我可以在其中将文本写入 RichTextBox,并使用一些控件来设置所写文本的字体格式。我能够将文本保存并附加到 RTF 文件,但我遇到了保留每个 RichTextBox的字体格式的问题。有什么帮助吗?
代码:
RichTextBox r1 = new RichTextBox();
RichTextBox r2 = new RichTextBox();
string nickName = "Test: ";
string message = "Hi this is a test message";
r1.Text = nickName;
r1.ForeColor = Color.Blue;
r2.Text = message;
r2.ForeColor = Color.Black;
string path = @"d:\Test.rtf";
if (!File.Exists(path))
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(r1.Rtf);
sw.WriteLine(r2.Rtf);
sw.Close();
}
}
else
{
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(r1.Rtf);
sw.WriteLine(r2.Rtf);
sw.Close();
}
}