RichTextBox.SaveFile 메서드

정의

RichTextBox의 내용을 파일로 저장합니다.

오버로드

SaveFile(Stream, RichTextBoxStreamType)

RichTextBox 컨트롤의 내용을 열려 있는 데이터 스트림으로 저장합니다.

SaveFile(String, RichTextBoxStreamType)

RichTextBox 컨트롤의 내용을 특정 형식의 파일로 저장합니다.

SaveFile(String)

RichTextBox의 내용을 RTF(서식 있는 텍스트 형식) 파일로 저장합니다.

SaveFile(Stream, RichTextBoxStreamType)

RichTextBox 컨트롤의 내용을 열려 있는 데이터 스트림으로 저장합니다.

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

저장할 파일이 들어 있는 데이터 스트림입니다.

fileType
RichTextBoxStreamType

RichTextBoxStreamType 값 중 하나입니다.

예외

fileType 매개 변수에 지정된 파일 형식이 잘못된 경우

컨트롤의 내용을 파일에 저장하는 동안 오류가 발생한 경우

예제

다음 코드 예제는 SaveFileLoadFile 스트림과 메서드. 사용 하는 방법도 보여 줍니다 합니다 FileDialog.FileName, FileDialog.DefaultExtSaveFileDialog.CreatePrompt, 및 SaveFileDialog.OverwritePrompt 멤버입니다.

이 프로젝트에 복사할 때 실행할 준비가 되는 전체 예제입니다.

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 메서드를 사용하면 컨트롤의 전체 내용을 이미 열려 있는 데이터 스트림에 저장할 수 있습니다. 그런 다음 데이터 스트림은 정보를 파일에 저장할 수 있습니다. 이 메서드를 LoadFile 사용하여 파일의 내용을 .에 로드할 RichTextBox수 있습니다.

이 버전의 SaveFile 메서드를 사용하면 개체로 전송 Stream 될 정보의 데이터 형식을 지정할 수도 있습니다.

추가 정보

적용 대상

SaveFile(String, RichTextBoxStreamType)

RichTextBox 컨트롤의 내용을 특정 형식의 파일로 저장합니다.

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

저장할 파일의 이름 및 위치입니다.

fileType
RichTextBoxStreamType

RichTextBoxStreamType 값 중 하나입니다.

예외

fileType 매개 변수에 지정된 파일 형식이 잘못된 경우

컨트롤의 내용을 파일에 저장하는 동안 오류가 발생한 경우

예제

다음 코드 예제에서는 ASCII 텍스트 파일에 내용을 저장 합니다 RichTextBox . 예제에서는 SaveFileDialog 클래스 경로 파일 이름을 사용자 로부터 요청 대화 상자를 표시 합니다. 코드는 컨트롤의 내용을 해당 파일에 저장합니다. 이 예제에서는 이 버전의 SaveFile 메서드를 사용하여 파일이 표준 서식 있는 텍스트 형식 대신 ASCII 텍스트 파일로 저장되도록 지정합니다. 이 예제에서는 코드가 명명richTextBox1된 컨트롤이 있는 Form 클래스에 RichTextBox 배치되어야 합니다.

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 WordPad와 같은 다른 프로그램에서 사용할 수 있는 RTF 파일에 저장할 수 있습니다. 매개 변수에 path 전달된 파일 이름이 지정된 디렉터리에 이미 있는 경우 예고 없이 파일을 덮어씁니다. 이 메서드를 LoadFile 사용하여 파일의 내용을 .에 로드할 RichTextBox수 있습니다.

이 버전의 SaveFile 메서드를 사용하면 컨트롤의 내용을 저장할 파일 형식을 지정할 수 있습니다. 이 기능을 사용하여 파일이 컨트롤의 내용에 따라 적절한 형식으로 저장되도록 할 수 있습니다. 예를 들어 문서에서 글꼴 스타일이나 색 지정에 차이가 없는 경우 매개 변수RichTextBoxStreamType.PlainText를 으로 설정 fileType 하여 파일을 ASCII 텍스트 파일로 저장할 수 있습니다.

추가 정보

적용 대상

SaveFile(String)

RichTextBox의 내용을 RTF(서식 있는 텍스트 형식) 파일로 저장합니다.

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

매개 변수

path
String

저장할 파일의 이름 및 위치입니다.

예외

컨트롤의 내용을 파일에 저장하는 동안 오류가 발생한 경우

예제

다음 코드 예제에서는 RTF 파일에 컨트롤의 RichTextBox 내용을 저장 합니다. 이 예제에서는 클래스를 SaveFileDialog 사용하여 사용자가 요청할 대화 상자, 저장할 파일의 경로 및 파일 이름을 표시합니다. 그런 다음 코드는 콘텐츠가 서식 있는 텍스트 형식이라고 가정하여 파일을 저장합니다. 파일이 이미 있는 경우 자동으로 덮어씁니다. 이 예제에서는 코드가 명명richTextBox1된 컨트롤이 있는 Form 클래스에 RichTextBox 배치되어야 합니다.

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 WordPad와 같은 다른 프로그램에서 사용할 수 있는 RTF 파일에 저장할 수 있습니다. 매개 변수에 path 전달된 파일 이름이 지정된 디렉터리에 이미 있는 경우 예고 없이 파일을 덮어씁니다. 이 메서드를 LoadFile 사용하여 파일의 내용을 .에 로드할 RichTextBox수 있습니다.

참고

컨트롤의 내용을 ASCII 텍스트와 같은 다른 형식의 파일 형식에 저장하려면 열거형의 값을 RichTextBoxStreamType 매개 변수로 허용하는 이 메서드의 다른 버전을 사용합니다.

추가 정보

적용 대상