FileDialog.FilterIndex 속성

정의

파일 대화 상자에서 현재 선택한 필터의 인덱스를 가져오거나 설정합니다.

public:
 property int FilterIndex { int get(); void set(int value); };
public int FilterIndex { get; set; }
member this.FilterIndex : int with get, set
Public Property FilterIndex As Integer

속성 값

Int32

파일 대화 상자에서 현재 선택한 필터의 인덱스를 포함하는 값입니다. 기본값은 1입니다.

예제

다음 코드 예제에서는 구현 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

설명

FilterIndex 속성을 사용하여 사용자에게 먼저 표시되는 필터링 옵션을 설정합니다. 선택한 필터에 따라 파일 대화 상자를 표시한 후의 FilterIndex 값을 사용하여 특수 파일 작업을 수행할 수도 있습니다.

참고

첫 번째 필터 항목의 인덱스 값은 1입니다.

적용 대상

추가 정보