WPF SaveFileDialog DefaultExt ignored?

DefaultExt is the extension that will be used if the user selects a file name with no extension (atleast that's my understanding from reading the description from MSDN).

When the user of your application specifies a file name without an extension, the FileDialog appends an extension to the file name.

You may have to make bmp the first item in the filter list.


You should set FilterIndex property instead of DefaultExt. If you still want to use DefaultExt, you can convert it to proper filter index manually:

public static void UseDefaultExtAsFilterIndex(FileDialog dialog)
{
    var ext = "*." + dialog.DefaultExt;
    var filter = dialog.Filter;
    var filters = filter.Split('|');
    for(int i = 1; i < filters.Length; i += 2)
    {
        if(filters[i] == ext)
        {
            dialog.FilterIndex = 1 + (i - 1) / 2;
            return;
        }
    }
}

var dlg = new SaveFileDialog();
dlg.FileName = "graph";
dlg.DefaultExt = ".bmp";
dlg.Filter = "PNG|*.png|DOT|*.dot|Windows Bitmap Format|*.bmp|GIF|*.gif|JPEG|*.jpg|PDF|*.pdf|Scalable Vector Graphics|*.svg|Tag Image File Format|*.tiff";
UseDefaultExtAsFilterIndex(dlg);
dlg.ShowDialog();