FileDialog.Filter 屬性

定義

取得或設定篩選條件字串,可決定在 OpenFileDialogSaveFileDialog 中顯示的檔案類型。

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。 預設值為 Empty,表示不套用任何篩選條件,而會顯示所有檔案類型。

例外狀況

此篩選條件字串無效。

範例

下列範例示範數種類型的篩選字串,這些字串可以使用 屬性來設定 Filter

OpenFileDialog dlg = new OpenFileDialog();

// Show all files
dlg.Filter = string.Empty;

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Show all files
dlg.Filter = String.Empty

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Show all files
dlg.Filter = null;

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Show all files
dlg.Filter = Nothing

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by Word Documents
dlg.Filter = "Word Documents|*.doc";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by Word Documents
dlg.Filter = "Word Documents|*.doc"

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by Excel Worksheets
dlg.Filter = "Excel Worksheets|*.xls";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by Excel Worksheets
dlg.Filter = "Excel Worksheets|*.xls"

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by PowerPoint Presentations
dlg.Filter = "PowerPoint Presentations|*.ppt";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by PowerPoint Presentations
dlg.Filter = "PowerPoint Presentations|*.ppt"

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by Office Files
dlg.Filter = "Office Files|*.doc;*.xls;*.ppt";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by Office Files
dlg.Filter = "Office Files|*.doc;*.xls;*.ppt"

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by All Files
dlg.Filter = "All Files|*.*";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by All Files
dlg.Filter = "All Files|*.*"

dlg.ShowDialog()
OpenFileDialog dlg = new OpenFileDialog();

// Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations 
//           OR Office Files 
//           OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" +
             "|Office Files|*.doc;*.xls;*.ppt" +
             "|All Files|*.*";

dlg.ShowDialog();
Dim dlg As New OpenFileDialog()

' Filter by Word Documents OR Excel Worksheets OR PowerPoint Presentations 
'           OR Office Files 
'           OR All Files
dlg.Filter = "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt" & "|Office Files|*.doc;*.xls;*.ppt" & "|All Files|*.*"

dlg.ShowDialog()

備註

如果 FilternullEmpty,則會顯示所有檔案,而且一律會顯示資料夾。

您可以藉由設定 Filter 屬性來指定要顯示的檔案類型子集。 每個檔案類型都可以代表特定類型的檔案,例如:

  • Word 檔 (*.doc)

  • Excel 工作表 (*.xls)

  • PowerPoint 簡報 (*.ppt)

或者,檔案類型可以代表一組相關的文件類型,例如:

  • Office 檔案 (*.doc、*.xls、*.ppt)

  • 所有檔案 (*.*)

若要指定顯示的檔案類型子集,您可以使用字串值來設定 Filter 屬性, (篩選字串) ,指定要篩選的一或多個文件類型。 以下顯示篩選字串的預期格式:

FileType1[[|FileType2]...[|FileTypeN]]

您可以使用下列格式來描述每個檔案類型:

Label|Extension1[[;Extension2]...[;ExtensionN]]

標籤是人類可讀取的字串值,可描述檔類型,如下所示:

  • “Word 檔”

  • “Excel 工作表”

  • “PowerPoint 簡報”

  • “Office 檔案”

  • “所有檔案”

每個檔案類型都必須由至少一個 擴展名描述。 如果使用多個擴充功能,每個延伸模組都必須以分號分隔 (“;”) 。 例如:

  • “*.doc”

  • “*.xls;”

  • “*.ppt”

  • “*.doc;*.xls;*.ppt”

  • "*.*"

以下是有效 Filter 字串值的完整範例:

  • Word Documents|*.doc

  • Excel Worksheets|*.xls

  • PowerPoint Presentations|*.ppt

  • Office Files|*.doc;*.xls;*.ppt

  • All Files|*.*

  • Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt|Office Files|*.doc;*.xls;*.ppt|All Files|*.*

篩選中包含的每個檔類型都會新增為個別專案至 或 SaveFileDialogOpenFileDialog類型的 [檔案]:下拉式清單,如下圖所示。

在開啟對話框中使用 FileDialog.Filter

用戶可以從此清單中選擇要篩選的檔案類型。 根據預設,清單中的第一個專案 (例如,在 或 顯示時OpenFileDialogSaveFileDialog會選取第一個檔類型) 。 若要指定要選取的另一個檔類型,您可以呼叫 ShowDialog) ,在顯示 OpenFileDialogSaveFileDialog (之前設定 FilterIndex 屬性。

適用於

另請參閱