Export (0) Print
Expand All

FileDialog.Filter Property

Gets or sets the filter string that determines what types of files are displayed from either the OpenFileDialog or SaveFileDialog.

Namespace:  Microsoft.Win32
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: Not mapped to an xmlns.

public string Filter { get; set; }
<object Filter="string" .../>

Property Value

Type: System.String
A String that contains the filter. The default is Empty, which means that no filter is applied and all file types are displayed.

ExceptionCondition
ArgumentException

The filter string is invalid.

If Filter is either null or Empty, all files are displayed, and folders are always displayed.

You can specify a subset of file types to be displayed by setting the Filter property. Each file type can represent a specific type of file, such as the following:

  • Word Documents (*.doc)

  • Excel Worksheets (*.xls)

  • PowerPoint Presentations (*.ppt)

Alternatively, a file type can represent a group of related file types, such as the following:

  • Office Files (*.doc, *.xls, *.ppt)

  • All Files (*.*)

To specify a subset of the types of files that are displayed, you set the Filter property with a string value (the filter string) that specifies one or more types of files to filter by. The following shows the expected format of the filter string:

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

You use the following format to describe each file type:

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

The Label part is a human-readable string value that describes the file type, such as the following:

  • "Word Documents"

  • "Excel Worksheets"

  • "PowerPoint Presentations"

  • "Office Files"

  • "All Files"

Each file type must be described by at least one Extension. If more than one Extension is used, each Extension must be separated by a semicolon (";"). For example:

  • "*.doc"

  • "*.xls;"

  • "*.ppt"

  • "*.doc;*.xls;*.ppt"

  • "*.*"

The following are complete examples of valid Filter string values:

  • 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|*.*

Each file type that is included in the filter is added as a separate item to the Files of type: drop-down list in the OpenFileDialog or SaveFileDialog, as shown in the following figure.

Using the FileDialog.Filter in the Open Dialog Box

The user can choose a file type from this list to filter by. By default, the first item in the list (for example, the first file type) is selected when the OpenFileDialog or SaveFileDialog is displayed. To specify that another file type to be selected, you set the FilterIndex property before showing the OpenFileDialog or SaveFileDialog (by calling ShowDialog).

The following examples demonstrate several types of filter strings that can be set by using the Filter property.

using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

// Show all files
dlg.Filter = null;

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


OpenFileDialog dlg = new OpenFileDialog();

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

dlg.ShowDialog();
using Microsoft.Win32;


...


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();

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft