OpenFileDialog.Filter Property
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Gets or sets a filter string that specifies the file types and descriptions to display in the OpenFileDialog.
Namespace: System.Windows.Controls
Assembly: System.Windows (in System.Windows.dll)
Syntax
'Declaration
Public Property Filter As String
public string Filter { get; set; }
Property Value
Type: System.String
A filter string that specifies the file types and descriptions to display in the OpenFileDialog. The default is Empty.
Exceptions
Exception | Condition |
---|---|
ArgumentException | The filter string does not contain at least one vertical bar (|). -or- The filter extension does not contain the wildcard character (*). (Silverlight 4 only) |
Remarks
If the Filter property is Empty, all files are displayed. Folders are always displayed.
Filter consists of a description of the filter followed by a vertical bar (|) and the filter pattern. The filter can specify one or more file types.
The description describes the type of files shown in the dialog box. Although the description can be any string, it usually consists of the type of files included in the filter followed by the parentheses that contain the extensions associated with the description. The filter description appears in the drop-down list of the dialog box. The following is an example of a filter description.
My Files (*.my)
The filter pattern determines which files are shown by the dialog box. Filter patterns for the same description are separated by a semicolon (;). You can specify an exact match or use the wildcard character (*) in combination with the dot character (.) to specify file name or extension matches.
Note: |
---|
Silverlight 4 requires the wildcard character in the extension portion of the filter. Not using one will cause an exception |
The following is an example of a filter description followed by multiple filter patterns. This example adds Image Files (*.bmp, *.jpg) to the drop-down list and shows .bmp and .jpg files when it is selected.
Image Files (*.bmp, *.jpg)|*.bmp;*.jpg
Multiple filter options are separated by the vertical bar.
The following is an example of multiple filter descriptions and patterns. This example adds Text Files (*.txt) and All Files (*.*) to the drop-down list. When Text Files (*.txt) is selected, .txt files are shown. When All Files (*.*) is selected, all file types are shown.
Text Files (*.txt)|*.txt|All Files (*.*)|*.*
Use the FilterIndex property to specify which filter appears first in the drop-down list. Otherwise, filter descriptions will appear in the order that they are listed in the Filter value.
If the specified filter does not contain at least one vertical bar, an exception will occur.
Note: |
---|
Do not put spaces before or after the vertical bars in the filter string. This will cause incorrect behavior in the filter. |
Examples
The following example shows how to create an OpenFileDialog and set the Filter and FilterIndex properties.
' Create an instance of the open file dialog box.
Dim openFileDialog1 As OpenFileDialog = New OpenFileDialog
' Set filter options and filter index.
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
// Create an instance of the open file dialog box.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
Version Information
Silverlight
Supported in: 5, 4, 3
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
See Also