Simplifying filter strings for file dialogs in WinForms and WPF
Using my FluentFileFilterBuilder library to easily craft file filters for the WinForms and WPF file dialogs.

The various file dialogs in WinForms (OpenFileDialog / SaveFileDialog) and WPF (OpenFileDialog / SaveFileDialog) all use file filter strings to control how they filter files. These strings are set in the Filter property of the various dialogs.

File filter strings are described as follows:

"For each filtering option, the filter string contains a description of the filter, followed by the vertical bar (|) and the filter pattern. The strings for different filtering options are separated by the vertical bar."

Here are some example filter strings:

  • Text files (*.txt)|*.txt|All files (*.*)|*.*
  • Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*

The exact format of these filter strings is sometimes hard to remember, and definitely hard to read.

I've created the FluentFileFilterBuilder library to simplify the task of working with these file filter strings.

Using FluentFileFilterBuilder, the example file filter strings above can be created like so:

var filter = new FileFilterBuilder()
    .Add("Text files", "txt")
    .Add("All files", "*")
    .ToString();
var filter = new FileFilterBuilder()
    .Add("Image files", "BMP", "JPG", "GIF")
    .Add("All files", "*")
    .ToString();

FluentFileFilterBuilder is available on NuGet under the MIT license:

PM> Install-Package FluentFileFilterBuilder
dotnet add package FluentFileFilterBuilder
paket add FluentFileFilterBuilder

I hope you find FluentFileFilterBuilder useful in your WinForms or WPF projects!


Posted by Matthew King on 11 March 2018
Permission is granted to use all code snippets under CC BY-SA 3.0 (just like StackOverflow), or the MIT license - your choice!
If you enjoyed this post, and you want to show your appreciation, you can buy me a beverage on Ko-fi or Stripe