Click here to Skip to main content
13,146,315 members (45,528 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 31 Jul 2007

File Dialog Filter Builder

, 31 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Helper class for building filter string for Windows file dialogs


The Windows file dialogs provide the Filter property to specify which kinds of files should be shown in the dialog. The syntax is pretty simple, but there is still scope to make a mistake and "create" a bug (with typo for example).

Using the Code

The FileDialogFilterBuilder class uses the FilterInfo structure for every filter item. The FilterInfo structure contains all information about one filter item. Using these two types are pretty simple and intuitive:

// Create builder
FileDialogFilterBuilder filterBuilder = new FileDialogFilterBuilder();
// add filter item for Word documents
filterBuilder.Infos.Add( new FilterInfo( "Word", "doc", "docx", "rtf" ) );
// add filter item for Excel documents
filterBuilder.Infos.Add( new FilterInfo( "Excel", "xls", "xlsx", "csv" ) );

// create filter item for images
FilterInfo infoImages = new FilterInfo( "Images" );
// these extensions will be used by dialog for filtering files
infoImages.Extensions = new string[] 
	{ "bmp", "jpg", "gif", "jpeg", "png", "wmf", "emf", "ico" };
// these extensions will be displayed to user with item
infoImages.VisibleExtensions = new string[] { "bmp", "jpg", "gif" };
// add item to builder
filterBuilder.Infos.Add( infoImages );

// add item with no-filter, with title "All file types"
filterBuilder.AddAllFileTypes( "All file types" );

using ( OpenFileDialog ofd = new OpenFileDialog() ) {
    // method "ToFilterString()" builds the filter string with correct syntax
    ofd.Filter = filterBuilder.ToFilterString();
    ofd.ShowDialog( this );

That's all.


  • 1st August, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jakub Mller
Web Developer
Czech Republic Czech Republic
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralOrdering and Default Pin
Member 334239818-Aug-09 12:25
memberMember 334239818-Aug-09 12:25 
GeneralSystem extension description Pin
Chris Richner1-Aug-07 2:06
memberChris Richner1-Aug-07 2:06 
GeneralRe: System extension description Pin
Jakub Müller1-Aug-07 2:44
memberJakub Müller1-Aug-07 2:44 
AnswerRe: System extension description Pin
Chris Richner1-Aug-07 12:26
memberChris Richner1-Aug-07 12:26 
GeneralRe: System extension description Pin
Ant210027-Oct-09 9:46
memberAnt210027-Oct-09 9:46 
Generalspelling mistake Pin
Michael Sync31-Jul-07 22:27
memberMichael Sync31-Jul-07 22:27 
GeneralRe: spelling mistake Pin
Jakub Müller31-Jul-07 22:45
memberJakub Müller31-Jul-07 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 1 Aug 2007
Article Copyright 2007 by Jakub Mller
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid