방법: OpenFileDialog 구성 요소를 사용하여 파일 열기

OpenFileDialog 구성 요소를 사용하면 사용자 컴퓨터 또는 네트워크로 연결된 모든 컴퓨터에서 폴더를 찾아보고 이 중에서 열려는 파일을 하나 이상 선택할 수 있습니다. 이 때 사용자가 대화 상자에서 선택한 파일의 이름과 경로가 대화 상자에 반환됩니다.

대상 파일을 선택한 후에는 두 가지 방법을 사용하여 파일을 열 수 있습니다. 파일 스트림을 사용하려면 StreamReader 클래스의 인스턴스를 만듭니다. 또는 OpenFile 메서드를 사용하여 선택된 파일을 열 수도 있습니다.

아래의 첫 번째 예제에서는 "보안 참고 사항"에 설명되어 있는 것처럼 FileIOPermission 권한 검사를 수행하지만 파일 이름에 액세스할 수 있습니다. 이 방법은 로컬 컴퓨터, 인트라넷 및 인터넷 영역에서 사용할 수 있습니다. 두 번째 방법도 FileIOPermission 권한 검사를 수행하지만 인트라넷 또는 인터넷 영역의 응용 프로그램에 더 적합합니다.

OpenFileDialog 구성 요소를 사용하여 파일을 스트림으로 열려면

  • 파일 열기 대화 상자를 표시하고 메서드를 호출하여 사용자가 선택한 파일을 엽니다.

    ShowDialog 메서드를 사용하여 파일 열기 대화 상자를 표시하고 StreamReader 클래스의 인스턴스를 사용하여 파일을 열 수 있습니다.

    아래 예제에서는 Button 컨트롤의 Click 이벤트 처리기를 사용하여 OpenFileDialog 구성 요소의 인스턴스를 엽니다. 파일이 선택된 상태에서 사용자가 확인을 클릭하면 대화 상자에서 선택된 파일이 열립니다. 이런 경우 파일 스트림을 읽었다는 것을 나타내기 위해 내용이 메시지 상자에 표시됩니다.

    보안 정보보안 정보

    FileName 속성을 가져오거나 설정하려면 어셈블리에 System.Security.Permissions.FileIOPermission 클래스에서 부여하는 권한 수준이 필요합니다. 부분 신뢰 컨텍스트에서 실행 중인 경우에는 권한이 부족하여 프로세스에서 예외를 throw할 수 있습니다. 자세한 내용은 코드 액세스 보안 기본 사항을 참조하십시오.

    이 예제에서는 폼에 Button 컨트롤과 OpenFileDialog 구성 요소가 있다고 가정합니다.

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
         Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
         MessageBox.Show(sr.ReadToEnd)
         sr.Close()
       End If
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
          System.IO.StreamReader sr = new 
             System.IO.StreamReader(openFileDialog1.FileName);
          MessageBox.Show(sr.ReadToEnd());
          sr.Close();
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
          {
             System::IO::StreamReader ^ sr = gcnew
                System::IO::StreamReader(openFileDialog1->FileName);
             MessageBox::Show(sr->ReadToEnd());
             sr->Close();
          }
       }
    

    (Visual C# 및 Visual C++) 폼의 생성자에 다음 코드를 배치하여 이벤트 처리기를 등록합니다.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this->button1->Click += gcnew
       System::EventHandler(this, &Form1::button1_Click);
    

    참고

    파일 스트림에서 읽는 데 대한 자세한 내용은 FileStream.BeginRead 메서드FileStream.Read 메서드를 참조하십시오.

OpenFileDialog 구성 요소를 사용하여 파일을 파일로 열려면

  • ShowDialog 메서드를 사용하여 대화 상자를 표시하고 OpenFile 메서드를 사용하여 파일을 엽니다.

    OpenFileDialog 구성 요소의 OpenFile 메서드는 파일을 구성하는 바이트를 반환합니다. 이러한 바이트는 읽을 스트림을 제공합니다. 아래 예제에서는 OpenFileDialog 구성 요소가 "cursor" 필터와 함께 인스턴스화되므로 사용자는 파일 확장명이 .cur인 파일만 선택할 수 있습니다. .cur 파일이 선택되면 폼의 커서가 선택된 커서로 설정됩니다.

    보안 정보보안 정보

    OpenFile 메서드를 호출하려면 어셈블리에 System.Security.Permissions.FileIOPermission 클래스에서 부여하는 권한 수준이 필요합니다. 부분 신뢰 컨텍스트에서 실행 중인 경우에는 권한이 부족하여 프로세스에서 예외를 throw할 수 있습니다. 자세한 내용은 코드 액세스 보안 기본 사항을 참조하십시오.

    이 예제에서는 폼에 Button 컨트롤이 있다고 가정합니다.

    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
       ' Displays an OpenFileDialog so the user can select a Cursor.
       Dim openFileDialog1 As New OpenFileDialog()
       openFileDialog1.Filter = "Cursor Files|*.cur"
       openFileDialog1.Title = "Select a Cursor File"
    
       ' Show the Dialog.
       ' If the user clicked OK in the dialog and 
       ' a .CUR file was selected, open it.
       If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
         ' Assign the cursor in the Stream to the Form's Cursor property.
         Me.Cursor = New Cursor(openFileDialog1.OpenFile())
       End If
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       // Displays an OpenFileDialog so the user can select a Cursor.
       OpenFileDialog openFileDialog1 = new OpenFileDialog();
       openFileDialog1.Filter = "Cursor Files|*.cur";
       openFileDialog1.Title = "Select a Cursor File";
    
       // Show the Dialog.
       // If the user clicked OK in the dialog and
       // a .CUR file was selected, open it.
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
       {
          // Assign the cursor in the Stream to the Form's Cursor property.
          this.Cursor = new Cursor(openFileDialog1.OpenFile());
       }
    }
    
    private:
       void button1_Click(System::Object ^ sender,
          System::EventArgs ^ e)
       {
          // Displays an OpenFileDialog so the user can select a Cursor.
          OpenFileDialog ^ openFileDialog1 = new OpenFileDialog();
          openFileDialog1->Filter = "Cursor Files|*.cur";
          openFileDialog1->Title = "Select a Cursor File";
    
          // Show the Dialog.
          // If the user clicked OK in the dialog and
          // a .CUR file was selected, open it.
          if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
          {
             // Assign the cursor in the Stream to
             // the Form's Cursor property.
             this->Cursor = gcnew
                System::Windows::Forms::Cursor(
                openFileDialog1->OpenFile());
          }
       }
    

    (Visual C# 및 Visual C++) 폼의 생성자에 다음 코드를 배치하여 이벤트 처리기를 등록합니다.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this->button1->Click += gcnew
       System::EventHandler(this, &Form1::button1_Click);
    

참고 항목

참조

OpenFileDialog

기타 리소스

OpenFileDialog 구성 요소(Windows Forms)