【问题标题】:What is the correct way to create an RTF file in C#?在 C# 中创建 RTF 文件的正确方法是什么?
【发布时间】:2012-03-30 22:27:12
【问题描述】:

here 我看到它建议创建一个 RichTextBox 实例,然后使用 SaveFile(string path) 方法将文件放到硬盘上:

RichTextBox rtb = new RichTextBox();

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

它有效,但是,......这是应该怎么做的,我问,因为它似乎有点hackish?如果不是,那么正确的做法是什么。

【问题讨论】:

    标签: c# winforms file richtextbox save


    【解决方案1】:

    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);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-23
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2016-10-17
      • 2015-05-22
      • 2014-08-28
      相关资源
      最近更新 更多