RichTextBox.SaveFile RichTextBox.SaveFile RichTextBox.SaveFile RichTextBox.SaveFile Method

定义

RichTextBox 的内容保存到文件。Saves the contents of the RichTextBox to a file.

重载

SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType)

RichTextBox 控件的内容保存到开放式数据流。Saves the contents of a RichTextBox control to an open data stream.

SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType)

RichTextBox 的内容保存到特定类型的文件中。Saves the contents of the RichTextBox to a specific type of file.

SaveFile(String) SaveFile(String) SaveFile(String) SaveFile(String)

RichTextBox 的内容保存到 RTF 格式文件。Saves the contents of the RichTextBox to a rich text format (RTF) file.

SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType) SaveFile(Stream, RichTextBoxStreamType)

RichTextBox 控件的内容保存到开放式数据流。Saves the contents of a RichTextBox control to an open data stream.

public:
 void SaveFile(System::IO::Stream ^ data, System::Windows::Forms::RichTextBoxStreamType fileType);
public void SaveFile (System.IO.Stream data, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.SaveFile : System.IO.Stream * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub SaveFile (data As Stream, fileType As RichTextBoxStreamType)

参数

data
Stream Stream Stream Stream

包含要保存到的文件的数据流。The data stream that contains the file to save to.

异常

fileType 参数中指定了无效的文件类型。An invalid file type is specified in the fileType parameter.

将控件内容保存到文件时出错。An error occurs in saving the contents of the control to a file.

示例

下面的代码示例演示如何将SaveFileLoadFile方法与流一起使用。The following code example demonstrates using the SaveFile and LoadFile methods with streams. 它还演示了如何FileDialog.FileName使用FileDialog.DefaultExtSaveFileDialog.CreatePrompt、和SaveFileDialog.OverwritePrompt成员。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 namespace System;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Windows::Forms;

public ref class Form1: public Form
{
public private:
   RichTextBox^ RichTextBox1;
   Button^ Button1;
   RichTextBox^ RichTextBox2;
   Button^ Button2;
   SaveFileDialog^ SaveFileDialog1;

public:
   Form1()
      : Form()
   {
      userInput = gcnew MemoryStream;
      this->RichTextBox1 = gcnew RichTextBox;
      this->Button1 = gcnew Button;
      this->RichTextBox2 = gcnew RichTextBox;
      this->Button2 = gcnew Button;
      this->SaveFileDialog1 = gcnew SaveFileDialog;
      this->SuspendLayout();
      this->RichTextBox1->Location = Point( 24, 64 );
      this->RichTextBox1->Name = "RichTextBox1";
      this->RichTextBox1->TabIndex = 0;
      this->RichTextBox1->Text = "Type something here.";
      this->Button1->Location = Point( 96, 16 );
      this->Button1->Name = "Button1";
      this->Button1->Size = Size( 96, 24 );
      this->Button1->TabIndex = 1;
      this->Button1->Text = "Save To Stream";
      this->Button1->Click += 
          gcnew EventHandler( this, &Form1::Button1_Click );
      this->RichTextBox2->Location = 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 = Point( 104, 200 );
      this->Button2->Name = "Button2";
      this->Button2->Size = Size( 88, 32 );
      this->Button2->TabIndex = 4;
      this->Button2->Text = "Save Stream To File";
      this->Button2->Click += 
          gcnew EventHandler( this, &Form1::Button2_Click );
      this->ClientSize = 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 );
   }

   // Declare a new memory stream.
   MemoryStream^ userInput;

private:

   // Save the content of RichTextBox1 to the memory stream, 
   // appending a LineFeed character.  
   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.
   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 )
      {
         fileStream = SaveFileDialog1->OpenFile();
         userInput->Position = 0;
         userInput->WriteTo( fileStream );
         fileStream->Close();
      }
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}

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();
        }
    }
}
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

Partial Public Class Form1
    Inherits Form

    Friend WithEvents RichTextBox1 As RichTextBox
    Friend WithEvents Button1 As Button
    Friend WithEvents RichTextBox2 As RichTextBox
    Friend WithEvents Button2 As Button
    Friend WithEvents SaveFileDialog1 As SaveFileDialog

    Public Sub New()
        MyBase.New()
        Me.RichTextBox1 = New RichTextBox
        Me.Button1 = New Button
        Me.RichTextBox2 = New RichTextBox
        Me.Button2 = New Button
        Me.SaveFileDialog1 = New SaveFileDialog
        Me.SuspendLayout()
        Me.RichTextBox1.Location = New Point(24, 64)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.TabIndex = 0
        Me.RichTextBox1.Text = "Type something here."
        Me.Button1.Location = New Point(96, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New Size(96, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Save To Stream"
        Me.RichTextBox2.Location = New Point(152, 64)
        Me.RichTextBox2.Name = "RichTextBox2"
        Me.RichTextBox2.TabIndex = 3
        Me.RichTextBox2.Text = "It will be added to the stream and appear here."
        Me.Button2.Location = New Point(104, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New Size(88, 32)
        Me.Button2.TabIndex = 4
        Me.Button2.Text = "Save Stream To File"
        Me.ClientSize = New Size(292, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.RichTextBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RichTextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub

    ' Declare a new memory stream.
    Dim userInput As New MemoryStream

    ' Save the content of RichTextBox1 to the memory stream, appending
    'a LineFeed character.  
    Private Sub Button1_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button1.Click
        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)
    End Sub

    ' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
    Private Sub Button2_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Button2.Click

        ' 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. 
        Dim result As DialogResult = SaveFileDialog1.ShowDialog()
        Dim fileStream As Stream

        If (result = DialogResult.OK) Then
            ' 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()
        End If
    End Sub

End Class

注解

此版本的SaveFile方法使你能够将控件的全部内容保存到已打开的数据流。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. 您可以使用LoadFile方法将文件的内容加载RichTextBox到中。You can use the LoadFile method to load the contents of a file into the RichTextBox.

此版本的SaveFile方法还允许您指定将发送Stream到对象的信息的数据格式。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.

安全性

FileIOPermission
用于创建或修改文件。for creating or modifying a file. 关联的枚举:WriteFileIOPermissionAccess值。Associated enumeration: The Write value of FileIOPermissionAccess.

另请参阅

SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType) SaveFile(String, RichTextBoxStreamType)

RichTextBox 的内容保存到特定类型的文件中。Saves the contents of the RichTextBox to a specific type of file.

public:
 void SaveFile(System::String ^ path, System::Windows::Forms::RichTextBoxStreamType fileType);
public void SaveFile (string path, System.Windows.Forms.RichTextBoxStreamType fileType);
member this.SaveFile : string * System.Windows.Forms.RichTextBoxStreamType -> unit
Public Sub SaveFile (path As String, fileType As RichTextBoxStreamType)

参数

path
String String String String

要保存的文件的名称和位置。The name and location of the file to save.

异常

fileType 参数中指定了无效的文件类型。An invalid file type is specified in the fileType parameter.

将控件内容保存到文件时出错。An error occurs in saving the contents of the control to a file.

示例

下面的代码示例将的RichTextBox内容保存到 ASCII 文本文件中。The following code example saves the contents of the RichTextBox into an ASCII text file. 该示例使用SaveFileDialog类来显示一个对话框,用于请求用户的路径和文件名。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. 该示例使用SaveFile方法的此版本来指定将文件另存为 ASCII 文本文件,而不是标准格式文本格式。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. 此示例要求将代码放置在一个Form RichTextBox具有名为richTextBox1的控件的类中。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 = gcnew 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 );
      }
   }
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);
   }
}

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 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) _
        And (saveFile1.FileName.Length) > 0 Then
    
        ' Save the contents of the RichTextBox into the file.
        richTextBox1.SaveFile(saveFile1.FileName, _
            RichTextBoxStreamType.PlainText)
    End If
End Sub

注解

使用SaveFile方法,可以将控件的全部内容保存到可由其他程序(如 Microsoft Word 和 Windows 写字板)使用的 RTF 文件中。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. 如果传递到path参数的文件名在指定的目录中已存在,则将在不通知的情况下覆盖该文件。If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. 您可以使用LoadFile方法将文件的内容加载RichTextBox到中。You can use the LoadFile method to load the contents of a file into the RichTextBox.

此版本的SaveFile方法使你可以指定要将控件的内容保存到的文件类型。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. 例如,如果文档的字体样式或着色没有差别,则可以通过将fileType参数设置为RichTextBoxStreamType.PlainText,将该文件另存为 ASCII 文本文件。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.

安全性

FileIOPermission
用于创建或修改文件。for creating or modifying a file. 关联的枚举:WriteFileIOPermissionAccess值。Associated enumeration: The Write value of FileIOPermissionAccess.

另请参阅

SaveFile(String) SaveFile(String) SaveFile(String) SaveFile(String)

RichTextBox 的内容保存到 RTF 格式文件。Saves the contents of the RichTextBox to a rich text format (RTF) file.

public:
 void SaveFile(System::String ^ path);
public void SaveFile (string path);
member this.SaveFile : string -> unit
Public Sub SaveFile (path As String)

参数

path
String String String String

要保存的文件的名称和位置。The name and location of the file to save.

异常

将控件内容保存到文件时出错。An error occurs in saving the contents of the control to a file.

示例

下面的代码示例将RichTextBox控件的内容保存到 RTF 文件中。The following code example saves the contents of a RichTextBox control to an RTF file. 该示例使用SaveFileDialog类来显示一个对话框,该对话框用于请求用户、要保存的文件的路径和文件名。The example uses the SaveFileDialog class to display a dialog to request from the user, the path and filename of the file to save. 然后,代码保存文件,假定内容为 rtf 格式。The code then saves the file assuming the content is in rich text format. 如果文件已存在,则会自动覆盖它。If the file already exists, it is automatically overwritten. 此示例要求将代码放置在一个Form RichTextBox具有名为richTextBox1的控件的类中。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 = 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 );
      }
   }
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);
   }
}

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

注解

使用SaveFile方法,可以将控件的全部内容保存到可由其他程序(如 Microsoft Word 和 Windows 写字板)使用的 RTF 文件中。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. 如果传递到path参数的文件名在指定的目录中已存在,则将在不通知的情况下覆盖该文件。If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. 您可以使用LoadFile方法将文件的内容加载RichTextBox到中。You can use the LoadFile method to load the contents of a file into the RichTextBox.

备注

若要将控件的内容保存到其他类型的文件格式(例如 ASCII 文本),请使用此方法的其他版本,该方法接受RichTextBoxStreamType枚举中的值作为参数。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.

另请参阅

适用于