FileDialog.Filter 속성

정의

대화 상자에서 "파일 형식으로 저장" 또는 "파일 형식" 상자에 표시되는 선택 옵션을 결정하는 현재 파일 이름 필터 문자열을 가져오거나 설정합니다.

public:
 property System::String ^ Filter { System::String ^ get(); void set(System::String ^ value); };
public string Filter { get; set; }
member this.Filter : string with get, set
Public Property Filter As String

속성 값

String

대화 상자에서 사용할 수 있는 파일 필터링 옵션입니다.

예외

Filter 형식이 잘못된 경우

예제

다음 코드 예제에서는 구현 FileDialogOpenFileDialog 사용 하 고 만들기를 보여 줍니다., 속성의 설정 및 대화 상자를 표시 합니다. 이 예제에서는 속성 FilterFilterIndex 속성을 사용하여 사용자에 대한 필터 목록을 제공합니다. 이 예제에서는 폼에 배치된 Button 양식과 네임스페이 System.IO 스가 추가되어야 합니다.

private:
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

      openFileDialog1->InitialDirectory = "c:\\";
      openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
      openFileDialog1->FilterIndex = 2;
      openFileDialog1->RestoreDirectory = true;

      if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
      {
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
         {
            // Insert code to read the stream here.
            myStream->Close();
         }
      }
   }
var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "c:\\";
    openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 2;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
        {
            fileContent = reader.ReadToEnd();
        }
    }
}

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If
        End Try
    End If
End Sub

설명

각 필터링 옵션에 대해 필터 문자열에는 필터에 대한 설명과 세로 막대(|) 및 필터 패턴이 포함됩니다. 다양 한 필터링 옵션에 대 한 문자열을 세로 막대로 구분 됩니다.

다음은 필터 문자열의 예입니다.

Text files (*.txt)|*.txt|All files (*.*)|*.*

파일 형식을 세미콜론으로 구분하여 필터에 여러 필터 패턴을 추가할 수 있습니다. 예를 들면 다음과 같습니다.

Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*

FilterIndex 속성을 사용하여 사용자에게 먼저 표시되는 필터링 옵션을 설정합니다.

적용 대상

추가 정보