24

In my application I use a SaveFileDialog to pop up a Save As window. I have restricted in the file type section the file to be saved as .dat with the following code.

sfdialog.Filter = "Data Files (*.dat*)|*.dat*";

What I want to know how to do is make it automatically save with the .dat extension. Currently it just saves with no extension unless I specifically save it as filename.dat.

novacara
  • 2,207
  • 4
  • 24
  • 34

2 Answers2

68
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Data Files (*.dat)|*.dat";
dlg.DefaultExt = "dat";
dlg.AddExtension = true;
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
Francis B.
  • 7,018
  • 2
  • 32
  • 54
6

The AddExtension and DefaultExt properties. For example:

sfdialog.DefaultExt = "dat";
sfdialog.AddExtension = true;
Number70
  • 453
  • 1
  • 4
  • 16
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • It's important to note that `DefaultExt` is required, setting just the `Filter` and `AddExtension` is not enough by itself, which I found strange. – Tyrrrz Nov 14 '18 at 09:01