Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF SaveFileDialog DefaultExt ignored?

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?

like image 308
mpen Avatar asked May 23 '11 23:05

mpen


2 Answers

I'm a few years too late, but coincidentally, I found a solution to the problem while looking at the code from this question.

There he specified the extension without a .. I then looked into the microsoft documentation. In the example the DefaultExt was also specified without a ..

If DefaultExt is specified with a ., FileDialog will automatically choose the first extension of the filter.

DefaultExt should be set to the extension without a ..
Meaning that in your example dlg.DefaultExt = ".bmp"; you need to change ".bmp" to "bmp"...

like image 76
DerRockWolf Avatar answered Oct 20 '22 07:10

DerRockWolf


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.

like image 20
Bala R Avatar answered Oct 20 '22 07:10

Bala R