Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-save-files-with-the-windows-forms-richtextbox-control.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

4.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Save Files with RichTextBox Control 03/30/2017
csharp
vb
cpp
saving files
RTF files [Windows Forms], saving in RichTextBox control
examples [Windows Forms], text boxes
saving files [Windows Forms], RichTextBox control
files [Windows Forms], saving with RichTextBox control
RichTextBox control [Windows Forms], saving files
.rtf files [Windows Forms], saving in RichTextBox control
text files [Windows Forms], saving from RichTextBox control
4a58ec19-84d1-4383-9110-298c06adcfca

How to: Save Files with the Windows Forms RichTextBox Control

The Windows Forms xref:System.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 xref:System.Windows.Forms.RichTextBox.SaveFile%2A method. You can also use the SaveFile method to save data to a stream. For more information, see xref:System.Windows.Forms.RichTextBox.SaveFile%28System.IO.Stream%2CSystem.Windows.Forms.RichTextBoxStreamType%29.

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 xref:System.Windows.Forms.SaveFileDialog component. For an overview, see SaveFileDialog Component Overview.

  2. Call the xref:System.Windows.Forms.RichTextBox.SaveFile%2A method of the xref:System.Windows.Forms.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 xref:System.Windows.Forms.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 xref:System.Windows.Forms.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(String::Concat
             (System::Environment::GetFolderPath
             (System::Environment::SpecialFolder::Personal),
             "\\Testdoc.rtf"), RichTextBoxStreamType::RichNoOleObjs);
       }
    

    Important

    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