Use file filters in an open file dialog or save file dialog in C#

The OpenFileDialog and SaveFileDialog components have a Filter property that lets the user look for specific kinds of files. The property's value should be a pipe symbol (|) separated list of alternating filter names and patterns. Each pattern can include more than one file matching pattern separated by semi-colons.
This example uses the following Filter:

Bitmaps|*.bmp|PNG files|*.png|JPEG files|*.jpg|Picture files|*.bmp;*.jpg;*.gif;*.png;*.tif|
CS files|*.cs|Project files|*.csproj|Program files|*.cs;*.csproj;*.sln;*.suo;*.resx

The parts of the filter are:

  • Bitmaps - *.bmp
  • PNG files - *.png
  • JPEG files - *.jpg
  • Picture files - *.bmp;*.jpg;*.gif;*.png;*.tif
  • CS files - *.cs
  • Project files - *.csproj
  • Program files - *.cs;*.csproj;*.sln;*.suo;*.resx

For example, if the user selects the "Picture files" filter, the dialog lists files with the extensions .bmp, .jpg, .gif, .png, and .tif.

The file selection dialogs also have a FilterIndex property thuat indicates the currently selected filter. You can set this to initially select a filter. Note, however, that the first filter's index is 1 instead of the 0 as is usually the case for arrays in .NET.

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.