HOW TO:使用 Windows Form RichTextBox 控制項儲存檔案

更新:2007 年 11 月

Windows Form RichTextBox 控制項可寫入以下列幾種格式顯示的資訊:

  • 純文字

  • Unicode 純文字

  • RTF

  • RTF,其中以空格代替 OLE 物件

  • 純文字加上 OLE 物件的文字表示

若要儲存檔案,請呼叫 SaveFile 方法。您也可使用 SaveFile 方法來將資料儲存至資料流。如需詳細資訊,請參閱 SaveFile(Stream, RichTextBoxStreamType)

若要將控制項的內容儲存至檔案

  1. 決定要儲存檔案的路徑。

    若要在實際應用程式中這麼做,您通常會使用 SaveFileDialog 元件。如需概觀說明,請參閱 SaveFileDialog 元件概觀 (Windows Form)

  2. 呼叫 RichTextBox 控制項的 SaveFile 方法,指定要儲存的檔案並選擇性地指定檔案類型。如果您只使用檔名當做呼叫方法的唯一引數,就會將檔案儲存為 RTF。若要指定其他檔案類型,請呼叫方法並將 RichTextBoxStreamType 列舉型別的任一值當做第二個引數。

    在以下範例中,為 RTF 檔位置設定的路徑為 [我的文件] 資料夾。這個位置的使用,是因為您可假設大部分執行 Windows 作業系統的電腦,都會包含這個資料夾。選擇這個位置也可讓使用者以最基本的系統存取層級,便可安全執行應用程式。下列範例假設已將 RichTextBox 控制項加入表單。

    Public Sub SaveFile()
       ' You should replace the bold file name in the 
       ' sample below with a file name of your own choosing.
       RichTextBox1.SaveFile(System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Testdoc.rtf", _
          RichTextBoxStreamType.RichNoOleObjs)
    End Sub
    
    public void SaveFile()
    {
       // You should replace the bold file name in the 
       // sample below with a file name of your own choosing.
       // Note the escape character used (@) when specifying the path.
       richTextBox1.SaveFile(System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Testdoc.rtf",
          RichTextBoxStreamType.RichNoOleObjs);
    }
    
    public void SaveFile()
    {
       // You should replace the bold file name in the 
       // sample below with a file name of your own choosing.
       richTextBox1.SaveFile(System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + "\\Testdoc.rtf",
          RichTextBoxStreamType.RichNoOleObjs);
    }
    
    public:
       void SaveFile()
       {
          // You should replace the bold file name in the 
          // sample below with a file name of your own choosing.
          richTextBox1->SaveFile(String::Concat
             (System::Environment::GetFolderPath
             (System::Environment::SpecialFolder::Personal),
             "\\Testdoc.rtf"), RichTextBoxStreamType::RichNoOleObjs);
       }
    
    安全性注意事項:

    如果檔案不存在,這個範例就會建立新檔案。如果應用程式需要建立檔案,該應用程式就需要資料夾的建立權限。使用權限是使用存取控制清單設定的。如果檔案已經存在,應用程式只需要寫入權限,這是較小的權限。若有可能,更為安全的做法是在部署期間建立檔案,並且只授與存取單一檔案的 Read 存取權,而不要授與存取資料夾的 Create 存取權。此外,較安全的做法是將資料寫入使用者資料夾,而不要寫入根資料夾或 [程式檔案] 資料夾。

請參閱

參考

RichTextBox.SaveFile

RichTextBox

其他資源

RichTextBox 控制項 (Windows Form)

在 Windows Form 上使用的控制項