RichTextBox.SaveFile Method

Definition

Saves the contents of the RichTextBox to a file.

Overloads

SaveFile(Stream, RichTextBoxStreamType)

Saves the contents of a RichTextBox control to an open data stream.

SaveFile(String, RichTextBoxStreamType)

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

SaveFile(String)

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

SaveFile(Stream, RichTextBoxStreamType)

Source:
RichTextBox.cs
Source:
RichTextBox.cs
Source:
RichTextBox.cs

Saves the contents of a RichTextBox control to an open data stream.

public void SaveFile (System.IO.Stream data, System.Windows.Forms.RichTextBoxStreamType fileType);

Parameters

data
Stream

The data stream that contains the file to save to.

fileType
RichTextBoxStreamType

One of the RichTextBoxStreamType values.

Exceptions

An invalid file type is specified in the fileType parameter.

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

Examples

The following code example demonstrates using the SaveFile and LoadFile methods with streams. It also demonstrates using the FileDialog.FileName, FileDialog.DefaultExt, SaveFileDialog.CreatePrompt, and SaveFileDialog.OverwritePrompt members.

This is a complete example that is ready to run when you copy it to your project.

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

public partial class Form1: Form
{
    internal RichTextBox RichTextBox1;
    internal Button Button1;
    internal RichTextBox RichTextBox2;
    internal Button Button2;
    internal SaveFileDialog SaveFileDialog1;

    public Form1() : base()
    {   
        this.RichTextBox1 = new RichTextBox();
        this.Button1 = new Button();
        this.RichTextBox2 = new RichTextBox();
        this.Button2 = new Button();
        this.SaveFileDialog1 = new SaveFileDialog();
        this.SuspendLayout();
        this.RichTextBox1.Location = new Point(24, 64);
        this.RichTextBox1.Name = "RichTextBox1";
        this.RichTextBox1.TabIndex = 0;
        this.RichTextBox1.Text = "Type something here.";
        this.Button1.Location = new Point(96, 16);
        this.Button1.Name = "Button1";
        this.Button1.Size = new Size(96, 24);
        this.Button1.TabIndex = 1;
        this.Button1.Text = "Save To Stream";
        this.Button1.Click += new EventHandler(Button1_Click);
        this.RichTextBox2.Location = new Point(152, 64);
        this.RichTextBox2.Name = "RichTextBox2";
        this.RichTextBox2.TabIndex = 3;
        this.RichTextBox2.Text = 
            "It will be added to the stream and appear here.";
        this.Button2.Location = new Point(104, 200);
        this.Button2.Name = "Button2";
        this.Button2.Size = new Size(88, 32);
        this.Button2.TabIndex = 4;
        this.Button2.Text = "Save Stream To File";
        this.Button2.Click += new EventHandler(Button2_Click);
        this.ClientSize = new Size(292, 266);
        this.Controls.Add(this.Button2);
        this.Controls.Add(this.RichTextBox2);
        this.Controls.Add(this.Button1);
        this.Controls.Add(this.RichTextBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }

    // Declare a new memory stream.
    MemoryStream userInput = new MemoryStream();

    // Save the content of RichTextBox1 to the memory stream, 
    // appending a LineFeed character.  
    private void Button1_Click(Object sender, EventArgs e)
    {
        RichTextBox1.SaveFile(userInput, RichTextBoxStreamType.PlainText);
        userInput.WriteByte(13);

        // Display the entire contents of the stream,
        // by setting its position to 0, to RichTextBox2.
        userInput.Position = 0;
        RichTextBox2.LoadFile(userInput, RichTextBoxStreamType.PlainText);
    }

    // Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    private void Button2_Click(Object sender, EventArgs e)
    {
        // Set the properties on SaveFileDialog1 so the user is 
        // prompted to create the file if it doesn't exist 
        // or overwrite the file if it does exist.
        SaveFileDialog1.CreatePrompt = true;
        SaveFileDialog1.OverwritePrompt = true;

        // Set the file name to myText.txt, set the type filter
        // to text files, and set the initial directory to the 
        // MyDocuments folder.
        SaveFileDialog1.FileName = "myText";
        // DefaultExt is only used when "All files" is selected from 
        // the filter box and no extension is specified by the user.
        SaveFileDialog1.DefaultExt = "txt";
        SaveFileDialog1.Filter = 
            "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        SaveFileDialog1.InitialDirectory = 
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        // Call ShowDialog and check for a return value of DialogResult.OK,
        // which indicates that the file was saved. 
        DialogResult result = SaveFileDialog1.ShowDialog();
        Stream fileStream;

        if (result == DialogResult.OK)
        {
            // Open the file, copy the contents of memoryStream to fileStream,
            // and close fileStream. Set the memoryStream.Position value to 0 
            // to copy the entire stream. 
            fileStream = SaveFileDialog1.OpenFile();
            userInput.Position = 0;
            userInput.WriteTo(fileStream);
            fileStream.Close();
        }
    }
}

Remarks

This version of the SaveFile method enables you to save the entire contents of the control to the data stream that is already opened. The data stream can then save the information to a file. You can use the LoadFile method to load the contents of a file into the RichTextBox.

This version of the SaveFile method also enables you to specify a data format of the information that will be sent to the Stream object.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

SaveFile(String, RichTextBoxStreamType)

Source:
RichTextBox.cs
Source:
RichTextBox.cs
Source:
RichTextBox.cs

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

public void SaveFile (string path, System.Windows.Forms.RichTextBoxStreamType fileType);

Parameters

path
String

The name and location of the file to save.

fileType
RichTextBoxStreamType

One of the RichTextBoxStreamType values.

Exceptions

An invalid file type is specified in the fileType parameter.

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

Examples

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

Remarks

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

SaveFile(String)

Source:
RichTextBox.cs
Source:
RichTextBox.cs
Source:
RichTextBox.cs

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

public void SaveFile (string path);

Parameters

path
String

The name and location of the file to save.

Exceptions

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

Examples

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.

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

Remarks

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.

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9