FileSaveDialog and default file extension

  • Thread starter Thread starter msnews.microsoft.com
  • Start date Start date
M

msnews.microsoft.com

Hi,

I am using System.Windows.Forms.SaveFileDialog to prompt the user to select a file to which data is exported.

I would like the control to always add the default extension, in this case .xls, to whatever filename the user enters - but the default behavior is that the extension is only added if the user has not already entered an extension.
E.g. if the user types in the filename "export.txt" then the SaveFileDialog should append .xls to form "export.txt.xls".

Of course I could just do this manually after the user has OK'd but then the dialog's prompt for create and overwrite won't be correct.

How can I configure SaveFileDialog to ALWAYS append .xls?

Below is a code snippet that I use to prompt the user for a filename:

public static string GetExportFilename()

{

System.Windows.Forms.SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.DefaultExt = "*.xls";

saveFileDialog.Filter = "Excel 8.0+ (*.xls)|*.xls";

saveFileDialog.AddExtension = true;

saveFileDialog.CheckPathExists = true;

saveFileDialog.CheckFileExists = false;

saveFileDialog.CreatePrompt = true;

saveFileDialog.OverwritePrompt = true;

string exportFilename = "";

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

exportFilename = saveFileDialog.FileName;



}



return exportFilename;

}


Regards
Joubert
 
If you want this done, you'll have to do it yourself. Be warned, this is a
non-standard behavior that you're trying to implement and is not a good idea
IMHO.

ShaneB
 
Hi Joubert,

It's funny that you ask this - I was just developing a system where my user saves .sql files for later loading and editing - I thought about the same thing!

I decided against forcing the .sql extension, but when I considered it, I thought I would simply look for a . in the filename; if it were present, I would determine if it was followed by only 'sql'; if not, I would append the .sql to the end of the filename.

HTH,

Bernie Yaeger

Hi,

I am using System.Windows.Forms.SaveFileDialog to prompt the user to select a file to which data is exported.

I would like the control to always add the default extension, in this case .xls, to whatever filename the user enters - but the default behavior is that the extension is only added if the user has not already entered an extension.
E.g. if the user types in the filename "export.txt" then the SaveFileDialog should append .xls to form "export.txt.xls".

Of course I could just do this manually after the user has OK'd but then the dialog's prompt for create and overwrite won't be correct.

How can I configure SaveFileDialog to ALWAYS append .xls?

Below is a code snippet that I use to prompt the user for a filename:

public static string GetExportFilename()

{

System.Windows.Forms.SaveFileDialog saveFileDialog = new SaveFileDialog();

saveFileDialog.DefaultExt = "*.xls";

saveFileDialog.Filter = "Excel 8.0+ (*.xls)|*.xls";

saveFileDialog.AddExtension = true;

saveFileDialog.CheckPathExists = true;

saveFileDialog.CheckFileExists = false;

saveFileDialog.CreatePrompt = true;

saveFileDialog.OverwritePrompt = true;

string exportFilename = "";

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

exportFilename = saveFileDialog.FileName;



}



return exportFilename;

}


Regards
Joubert
 
Hi Bernie,

I've also done this but then when the file already exists, the
SaveFileDialog does not prompt the user to overwrite. And if the file does
not exist, the SFDialog does not prompt the user to create.

Have a look at Excel (I'm using 2003) - add an extension to a file in the
Save dialog - the Save File Dialog then adds the .xls extension (not the
calling code).
This is what I'm trying to achieve....
 
Hi Shane,

Have a look at Excel (I'm using 2003) - add an extension to a file in the
Save dialog - the Save File Dialog then adds the .xls extension (not the
calling code).
This is what I'm trying to achieve....
 
Hi Joubert,

Yes, I see what you mean. Unfortunately, the savefiledialog, all of the
replacement common dialogs, are 'black boxes', which don't allow us to do
too much with it. I will try working with the dispose event to see if I can
trap it there, searching first for a 'file exists' etc.

I'll let you know what I discover; please let me know if you find a
solution.

Bernie
 
Hi Bernie,

Deal - I will also research a bit more and let you know what I find.

Cheers
Joubert
 
My apologies for the error...brainfart. Unfortunately, I believe you're
still stuck coding it yourself.

ShaneB
 
Hi Joubert,

Sorry to report, but I spent about 2 hours last night trying all kinds of
things and got nowhere. The dispose event was of no help at all.

Hope you have more luck than I did.

Bernie
 
Bernie,

Also no luck here. I am deferring this enhancement to my project till a
later stage.

Cheers
Joubert
 
Back
Top