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";
The extension always defaults to .png
. It seems the DefaultExt
is ignored if there is a Filter
; then it just defaults to the first option in the list.
Is there a way to force it to actually respect the default ext?
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();