RichTextBox.SaveFile Method (String, RichTextBoxStreamType)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Saves the contents of the RichTextBox to a specific type of file.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void SaveFile(
	string path,
	RichTextBoxStreamType fileType


Type: System.String

The name and location of the file to save.

Type: System.Windows.Forms.RichTextBoxStreamType

One of the RichTextBoxStreamType values.

Exception Condition

An invalid file type is specified in the fileType parameter.


An error occurs in saving the contents of the control to a file.

The SaveFile method enables you to save the entire contents of the control to an RTF file that can be used by other programs such as Microsoft Word and Windows WordPad. If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. You can use the LoadFile method to load the contents of a file into the RichTextBox.

This version of the SaveFile method enables you to specify a file type to save the contents of the control to. You can use this feature to ensure that the file is saved in the proper format based on the contents of the control. For example, if your document has no differences in font style or coloring, you can save the file as an ASCII text file by setting the fileType parameter to RichTextBoxStreamType.PlainText.

The following code example saves the contents of the RichTextBox into an ASCII text file. The example uses the SaveFileDialog class to display a dialog to request the path and file name from the user. The code then saves the contents of the control to that file. The example uses this version of the SaveFile method to specify that the file be saved as an ASCII text file instead of the standard rich text format. This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

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


for creating or modifying a file. Associated enumeration: The Write value of FileIOPermissionAccess.

.NET Framework
Available since 1.1
Return to top