我在这里看到建议创建RichTextBox实例,然后使用它的SaveFile(string path)方法将文件保存到硬盘上:
RichTextBox rtb = new RichTextBox();
rtb.SaveFile(@"\MyFileName.rtf");
它可以工作,但是...这是正确的做法吗?我问这个问题是因为它似乎有点hackish。如果不是,那么正确的做法是什么。
根据MSDN文档,这就是您应该这样做的方式。
他们还在这里提供了以下示例:
public void SaveMyFile()
{
// Create a SaveFileDialog to request a path and file name to save to.
SaveFileDialog saveFile1 = new SaveFileDialog();
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
// Determine if the user selected a file name from the saveFileDialog.
if(saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
}
}