2012-03-30 144 views
2

here我已经看到了建议,RichTextBox中的一个实例被创建,然后它的SAVEFILE(字符串路径)方法来获取文件到硬盘驱动器:在C#中创建RTF文件的正确方法是什么?

RichTextBox rtb = new RichTextBox(); 

rtb.SaveFile(@"\MyFileName.rtf"); 

它的工作原理,但是,.. ...这是应该怎么做,我问,因为它似乎有点hackish?如果不是,那么做这件事的正确方法是什么?

回答

2

MSDN documentation说这正是你应该这样做的。

他们也有下面的例子here

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); 
    } 
} 
相关问题