我有一个丰富的文本框,我也有一个特定的按钮,当单击时,该框应在特定位置将文本保存到RTF格式中。
我谷歌搜索,但没有找到适当的解决方案来实现文件保存而无需使用任何对话框!
我希望我在这里找到一些帮助,谢谢!
在保存或加载文件期间,对话框的唯一目的是获取继承人位置。如果您必须使用特定的位置 - 最终可以在代码中的某个地方使用一个简单的常数。只需确保您已经逃脱了斜线即可。
const string fileLocation = @"C:Folderfile.rtf";
所以,如果您使用的是Winforms,则可以使用RichTextbox.savefile:
richTextBox1.SaveFile(fileLocation );
,如果您使用的是WPF,则可以使用TexTrange.Save:
TextRange t = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
using (FileStream file = new FileStream(fileLocation, FileMode.Create))
{
t.Save(file, System.Windows.DataFormats.Rtf);
}
使用OnClick
事件和StreamWriter
public void button_Click(object sender, EventArgs e)
{
using(var sr = new StreamWriter(@"C:MyFilePathfile.rtf"))
{
sr.Write(rtf.Rtf);
}
}
这应该做技巧:
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\test\test.rtf");
file.WriteLine(this.richTextBox1.Rtf);
file.Close();
确保您使用.rtf为.Text不会包含丰富的文本格式代码。