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

Définition

Enregistre le contenu du contrôle RichTextBox dans un fichier.Saves the contents of the RichTextBox to a file.

Surcharges

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

Enregistre le contenu d'un contrôle RichTextBox dans un flux de données ouvert.Saves the contents of a RichTextBox control to an open data stream.

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

Enregistre le contenu du contrôle RichTextBox dans un type de fichier spécifique.Saves the contents of the RichTextBox to a specific type of file.

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

Enregistre le contenu de RichTextBox dans un fichier 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)

Enregistre le contenu d'un contrôle RichTextBox dans un flux de données ouvert.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)

Paramètres

data
Stream Stream Stream Stream

Flux de données qui contient le fichier de destination.The data stream that contains the file to save to.

Exceptions

Le paramètre fileType contient un type de fichier non valide.An invalid file type is specified in the fileType parameter.

une erreur s'est produite durant l'enregistrement du contenu du contrôle dans un fichier.An error occurs in saving the contents of the control to a file.

Exemples

L’exemple de code suivant illustre l' SaveFile utilisation LoadFile des méthodes et avec des flux.The following code example demonstrates using the SaveFile and LoadFile methods with streams. Il illustre également l’utilisation FileDialog.FileNamedes FileDialog.DefaultExtmembres SaveFileDialog.CreatePrompt,, SaveFileDialog.OverwritePrompt et.It also demonstrates using the FileDialog.FileName, FileDialog.DefaultExt, SaveFileDialog.CreatePrompt, and SaveFileDialog.OverwritePrompt members.

Il s’agit d’un exemple complet qui est prêt à s’exécuter lorsque vous le copiez dans votre projet.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

Remarques

Cette version de la SaveFile méthode vous permet d’enregistrer tout le contenu du contrôle dans le flux de données déjà ouvert.This version of the SaveFile method enables you to save the entire contents of the control to the data stream that is already opened. Le flux de données peut ensuite enregistrer les informations dans un fichier.The data stream can then save the information to a file. Vous pouvez utiliser la LoadFile méthode pour charger le contenu d’un fichier dans le RichTextBox.You can use the LoadFile method to load the contents of a file into the RichTextBox.

Cette version de la SaveFile méthode vous permet également de spécifier un format de données des informations qui seront envoyées à l' Stream objet.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.

Sécurité

FileIOPermission
pour créer ou modifier un fichier.for creating or modifying a file. Énumération associée : Write Valeur deFileIOPermissionAccess.Associated enumeration: The Write value of FileIOPermissionAccess.

Voir aussi

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

Enregistre le contenu du contrôle RichTextBox dans un type de fichier spécifique.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)

Paramètres

path
String String String String

Nom et emplacement du fichier à enregistrer.The name and location of the file to save.

Exceptions

Le paramètre fileType contient un type de fichier non valide.An invalid file type is specified in the fileType parameter.

une erreur s'est produite durant l'enregistrement du contenu du contrôle dans un fichier.An error occurs in saving the contents of the control to a file.

Exemples

L’exemple de code suivant enregistre le contenu de RichTextBox dans un fichier texte ASCII.The following code example saves the contents of the RichTextBox into an ASCII text file. L’exemple utilise la SaveFileDialog classe pour afficher une boîte de dialogue qui demande le chemin d’accès et le nom de fichier de l’utilisateur.The example uses the SaveFileDialog class to display a dialog to request the path and file name from the user. Le code enregistre ensuite le contenu du contrôle dans ce fichier.The code then saves the contents of the control to that file. L’exemple utilise cette version de la SaveFile méthode pour spécifier que le fichier doit être enregistré sous la forme d’un fichier texte ASCII au lieu du format de texte enrichi standard.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. Cet exemple requiert que le code soit placé dans une Form classe qui a un RichTextBox contrôle nommé 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

Remarques

La SaveFile méthode vous permet d’enregistrer tout le contenu du contrôle dans un fichier RTF qui peut être utilisé par d’autres programmes tels que Microsoft Word et Windows WordPad.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. Si le nom de fichier qui est passé au path paramètre existe déjà dans le répertoire spécifié, le fichier est remplacé sans avertissement.If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. Vous pouvez utiliser la LoadFile méthode pour charger le contenu d’un fichier dans le RichTextBox.You can use the LoadFile method to load the contents of a file into the RichTextBox.

Cette version de la SaveFile méthode vous permet de spécifier un type de fichier dans lequel enregistrer le contenu du contrôle.This version of the SaveFile method enables you to specify a file type to save the contents of the control to. Vous pouvez utiliser cette fonctionnalité pour vous assurer que le fichier est enregistré dans le format approprié en fonction du contenu du contrôle.You can use this feature to ensure that the file is saved in the proper format based on the contents of the control. Par exemple, si votre document n’a pas de différences de style de police ou de couleur, vous pouvez enregistrer le fichier en tant que fichier texte fileType ASCII en RichTextBoxStreamType.PlainTextaffectant au paramètre la valeur.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.

Sécurité

FileIOPermission
pour créer ou modifier un fichier.for creating or modifying a file. Énumération associée : Write Valeur deFileIOPermissionAccess.Associated enumeration: The Write value of FileIOPermissionAccess.

Voir aussi

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

Enregistre le contenu de RichTextBox dans un fichier 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)

Paramètres

path
String String String String

Nom et emplacement du fichier à enregistrer.The name and location of the file to save.

Exceptions

une erreur s'est produite durant l'enregistrement du contenu du contrôle dans un fichier.An error occurs in saving the contents of the control to a file.

Exemples

L’exemple de code suivant enregistre le contenu d' RichTextBox un contrôle dans un fichier RTF.The following code example saves the contents of a RichTextBox control to an RTF file. L’exemple utilise la SaveFileDialog classe pour afficher une boîte de dialogue permettant de demander à l’utilisateur, le chemin d’accès et le nom du fichier à enregistrer.The example uses the SaveFileDialog class to display a dialog to request from the user, the path and filename of the file to save. Le code enregistre ensuite le fichier en supposant que le contenu est au format de texte enrichi.The code then saves the file assuming the content is in rich text format. Si le fichier existe déjà, il est automatiquement remplacé.If the file already exists, it is automatically overwritten. Cet exemple requiert que le code soit placé dans une Form classe qui a un RichTextBox contrôle nommé 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

Remarques

La SaveFile méthode vous permet d’enregistrer tout le contenu du contrôle dans un fichier RTF qui peut être utilisé par d’autres programmes tels que Microsoft Word et Windows WordPad.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. Si le nom de fichier qui est passé au path paramètre existe déjà dans le répertoire spécifié, le fichier est remplacé sans avertissement.If the file name that is passed to the path parameter already exists at the specified directory, the file will be overwritten without notice. Vous pouvez utiliser la LoadFile méthode pour charger le contenu d’un fichier dans le RichTextBox.You can use the LoadFile method to load the contents of a file into the RichTextBox.

Notes

Pour enregistrer le contenu du contrôle dans un autre type de format de fichier, tel que du texte ASCII, utilisez les autres versions de cette méthode qui acceptent une valeur RichTextBoxStreamType de l’énumération en tant que paramètre.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.

Voir aussi

S’applique à