Curious SaveFileDialog gotcha

  • Thread starter Michael A. Covington
  • Start date
M

Michael A. Covington

I've just determined, by experimenting, that regardless of its Filter and
DefaultExt properties, a SaveFileDialog will let you save the file with
*any* registered extension.

For instance, if you specify a SaveFileDialog with the attributes:

AddExtension = true
Filter = "Foo files|*.foo"
DefaultExt = "foo"

and you type the file name qwerty, you get qwerty.foo, exactly as you might
expect.

And if you type qwerty.bla (where .bla is not a registered extension), you
get qwerty.bla.foo. Again so far so good.

But if you type the file name qwerty.exe, or qwerty.bat, or qwerty.doc, or
any other file name with a registered extension, then .foo is *not* appended
to it.

This does not strike me as entirely wise, and I haven't found any
documentation of it!

Is there another attribute that will disable it (so my file dialog will
really insist on file names ending in .foo)?

Thanks!



Michael A. Covington - Artificial Intelligence Ctr - University of Georgia

"In the core C# language it is simply not possible to have an uninitialized
variable, a 'dangling' pointer, or an expression that indexes an array
beyond its bounds. Whole categories of bugs that routinely plague C and C++
programs are thus eliminated." - A. Hejlsberg, The C# Programming Language
 
B

Brian W

IIRC this has been true ever since long filenames were introduced in Win
'95.

Having said that, I disagree, this DOES make sense. I'm not sure I can
coherently explain why, I'll have to think on that one.

Filter only populates the filetypes dropdown and provides a way to display
certain filetypes, it has nothing to do with the extension used when
creating the file name.

You may be able to change this behavior by inheriting your own class from
SaveFileDialog and overriding OnFileOk, but it may not be worth the trouble.



Regards
Brian W
 
M

Michael A. Covington

Brian W said:
IIRC this has been true ever since long filenames were introduced in Win
'95.

Having said that, I disagree, this DOES make sense. I'm not sure I can
coherently explain why, I'll have to think on that one.

Filter only populates the filetypes dropdown and provides a way to display
certain filetypes, it has nothing to do with the extension used when
creating the file name.

You may be able to change this behavior by inheriting your own class from
SaveFileDialog and overriding OnFileOk, but it may not be worth the
trouble.

But what surprised me is that it apparently looks into the registry to
decide what is and is not an extension.

If it had accepted any extension typed by the user, and only added one when
there was one, I would have understood it.
 
G

Guest

Here's a simple brute force way to accomplish this (VB.Net code - if that matters)

sFilePath = objSaveFileDialog.FileNam

If Not sFilePath.EndsWith(".foo") The
sFilePath &= ".foo
End I

HTH
Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top