SaveFileDialog.Filter Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets a filter string that specifies the files types and descriptions to display in the SaveFileDialog.

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 SaveFileDialog. The default is Empty.

Exceptions

Exception Condition
ArgumentException

The filter string does not contain at least one vertical bar (|).

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 Save as type 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.

The following is an example of a filter description followed by multiple filter patterns. This example adds Image Files (*.bmp, *.jpg) to the Save as type 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 Save as type 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 Save as type 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.

Examples

The following code example shows how to use the Filter property.

Private textDialog As SaveFileDialog
Public Sub New()
    InitializeComponent()
    textDialog = New SaveFileDialog()
    textDialog.Filter = "Text Files | *.txt"
    textDialog.DefaultExt = "txt"
End Sub

Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim result As System.Nullable(Of Boolean) = textDialog.ShowDialog()
    If result = True Then
        Dim fileStream As System.IO.Stream = textDialog.OpenFile()
        Dim sw As New System.IO.StreamWriter(fileStream)
        sw.WriteLine("Writing some text in the file.")
        sw.Flush()
        sw.Close()
    End If
End Sub
SaveFileDialog textDialog;
public Page()
{
    InitializeComponent();
    textDialog = new SaveFileDialog();
    textDialog.Filter = "Text Files | *.txt";
    textDialog.DefaultExt = "txt";
 }

private void button1_Click(object sender, RoutedEventArgs e)
{
    bool? result = textDialog.ShowDialog();
    if (result == true)
    {
        System.IO.Stream fileStream = textDialog.OpenFile();
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
        sw.WriteLine("Writing some text in the file.");
        sw.Flush();
        sw.Close();
    }
}
<Grid x:Name="LayoutRoot" Background="White">
     <Button Height="100" Width="100" Content="Save Text File" x:Name="button1" 
        Click="button1_Click"  />
</Grid>

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.