我有一个富文本框,还有一个用于富文本框的特定按钮,当单击该按钮时,应在特定位置将文本保存为rtf格式。
我谷歌了,但没有找到适当的解决方案来实现文件保存,而不使用任何对话框!
我希望我能在这里找到一些帮助,谢谢!
发布于 2014-01-22 07:38:32
保存或加载文件时,对话框的唯一用途是获取继承人位置。如果你必须使用特定的位置-你可以在你的代码中的某个地方使用一个简单的常量。只需确保您已对斜杠进行了转义。
const string fileLocation = @"C:\Folder\file.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);
}发布于 2014-01-22 07:36:24
使用OnClick事件和StreamWriter
public void button_Click(object sender, EventArgs e)
{
using(var sr = new StreamWriter(@"C:\MyFilePath\file.rtf"))
{
sr.Write(rtf.Rtf);
}
}发布于 2014-01-22 07:37:27
这应该能起到作用:
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test\\test.rtf");
file.WriteLine(this.richTextBox1.Rtf);
file.Close();请确保使用.Rtf,因为.Text不会包含.Rtf格式的代码。
https://stackoverflow.com/questions/21271277
复制相似问题