Поделиться через


How to: Save Files with the Windows Forms RichTextBox Control

The Windows Forms RichTextBox control can write the information it displays in one of several formats:

  • Plain text

  • Unicode plain text

  • Rich-Text Format (RTF)

  • RTF with spaces in place of OLE objects

  • Plain text with a textual representation of OLE objects

To save a file, call the SaveFile method. You can also use the SaveFile method to save data to a stream. For more information, see SaveFile(Stream, RichTextBoxStreamType).

To save the contents of the control to a file

  1. Determine the path of the file to be saved.

    To do this in a real-world application, you would typically use the SaveFileDialog component. For an overview, see SaveFileDialog Component Overview (Windows Forms).

  2. Call the SaveFile method of the RichTextBox control, specifying the file to save and optionally a file type. If you call the method with a file name as its only argument, the file will be saved as RTF. To specify another file type, call the method with a value of the RichTextBoxStreamType enumeration as its second argument.

    In the example below, the path set for the location of the rich-text file is the My Documents folder. This location is used because you can assume that most computers running the Windows operating system will include this folder. Choosing this location also allows users with minimal system access levels to safely run the application. The example below assumes a form with a RichTextBox control already added.

    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);
       }
    
    Security noteSecurity Note

    This example creates a new file, if the file does not already exist. If an application needs to create a file, that application needs Create access for the folder. Permissions are set using access control lists. If the file already exists, the application needs only Write access, a lesser privilege. Where possible, it is more secure to create the file during deployment, and only grant Read access to a single file, rather than Create access for a folder. Also, it is more secure to write data to user folders than to the root folder or the Program Files folder.

See Also

Reference

RichTextBox.SaveFile

RichTextBox

Other Resources

RichTextBox Control (Windows Forms)

Controls to Use on Windows Forms