Save file with appropriate extension in a Save File prompt
Asked Answered
D

2

26

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.

Diplomacy answered 31/7, 2009 at 15:50 Comment(0)
T
70
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Data Files (*.dat)|*.dat";
dlg.DefaultExt = "dat";
dlg.AddExtension = true;
Teem answered 31/7, 2009 at 15:53 Comment(0)
C
6

The AddExtension and DefaultExt properties. For example:

sfdialog.DefaultExt = "dat";
sfdialog.AddExtension = true;
Crozier answered 31/7, 2009 at 15:53 Comment(1)
It's important to note that DefaultExt is required, setting just the Filter and AddExtension is not enough by itself, which I found strange.Dwt

© 2022 - 2024 — McMap. All rights reserved.