Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
RichTextBox Class
RichTextBox Methods
SaveFile Method
 SaveFile Method (String)

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
RichTextBox.SaveFile Method (String)

Saves the contents of the RichTextBox to a rich text format (RTF) file.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Visual Basic (Declaration)
Public Sub SaveFile ( _
    path As String _
)
Visual Basic (Usage)
Dim instance As RichTextBox
Dim path As String

instance.SaveFile(path)
C#
public void SaveFile (
    string path
)
C++
public:
void SaveFile (
    String^ path
)
J#
public void SaveFile (
    String path
)
JScript
public function SaveFile (
    path : String
)

Parameters

path

The name and location of the file to save.

Exception typeCondition

IOException

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.

NoteNote

To save the contents of the control to a different type of file format such as ASCII text, use the other versions of this method that accept a value from the RichTextBoxStreamType enumeration as a parameter.

The following code example saves the contents of a RichTextBox control to an RTF file. The example uses the SaveFileDialog class to display a dialog to request from the user, the path and filename of the file to save. The code then saves the file assuming the content is in rich text format. If the file already exists, it is automatically overwritten. This example requires that the code is placed in a Form class that has a RichTextBox control named richTextBox1.

Visual Basic
Public Sub SaveMyFile()
    ' Create a SaveFileDialog to request a path and file name to save to.
    Dim saveFile1 As New SaveFileDialog()
    
    ' Initialize the SaveFileDialog to specify the RTF extention for the file.
    saveFile1.DefaultExt = "*.rtf"
    saveFile1.Filter = "RTF Files|*.rtf"
    
    ' Determine whether the user selected a file name from the saveFileDialog.
    If (saveFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
        And (saveFile1.FileName.Length > 0) Then
        
        ' Save the contents of the RichTextBox into the file.
        richTextBox1.SaveFile(saveFile1.FileName)
    End If
End Sub

C#
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 extention for the file.
   saveFile1.DefaultExt = "*.rtf";
   saveFile1.Filter = "RTF Files|*.rtf";

   // Determine whether 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);
   }
}

C++
public:
   void SaveMyFile()
   {
      // Create a SaveFileDialog to request a path and file name to save to.
      SaveFileDialog^ saveFile1 = gcnew SaveFileDialog;
      
      // Initialize the SaveFileDialog to specify the RTF extention for the file.
      saveFile1->DefaultExt = "*.rtf";
      saveFile1->Filter = "RTF Files|*.rtf";
      
      // Determine whether 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 );
      }
   }
J#
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 extention for the 
    // file.
    saveFile1.set_DefaultExt("*.rtf");
    saveFile1.set_Filter("RTF Files|*.rtf");

    // Determine whether the user selected a file name from the 
    // saveFileDialog.
    if (saveFile1.ShowDialog() == 
        System.Windows.Forms.DialogResult.OK && 
        saveFile1.get_FileName().length() > 0) {
            // Save the contents of the RichTextBox into the file.
            richTextBox1.SaveFile(saveFile1.get_FileName());
    }
} //SaveMyFile

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker