I've the following C# code, which is using the SaveFileDialog and set's the AddExtension property to true
:
var dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "txt";
dialog.Filter = "Text files (*.txt)|*.txt|XML files (*.xml)|*.xml";
dialog.OverwritePrompt = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = dialog.FileName;
}
And I've tested the following combination of File name
and Save as type
of the dialog.
File name | Save as type | label1.Text | What I expect
----------------+----------------+----------------+----------------
test1 | *.txt | test1.txt | test1.txt
test2.txt | *.txt | test2.txt | test2.txt
test3.abc | *.txt | test3.abc.txt | test3.abc.txt
test4 | *.xml | test4.xml | test4.xml
test5.xml | *.xml | test5.xml | test5.xml
test6.abc | *.xml | test6.abc.xml | test6.abc.xml
----------------+----------------+----------------+----------------
test7.xml | *.txt | test7.xml | test7.xml.txt
test8.bmp | *.txt | test8.bmp | test8.bmp.txt
test9.bmp | *.xml | test9.bmp | test9.bmp.xml
For the last three lines of the above table I would expect a double extension like it does for the unknown abc
extension. Applications like Microsoft Word behave like that (they always add the double extension if the Save as type
doesn't match the extension given by the user in File name
).
Is there a way to change that?
I don't want to do it after the dialog is closed because then I've to check again if the file already exists and if the file name is not too long.
Update:
I've tested it as well with the MONO framework using Ubuntu 18.04. In that case a double extension is never created, e.g.: test3.abc
using MONO vs test3.abc.txt
using .NET Framework 4.5 (Windows 10).
Source code on my Github (batressc)
In simple terms, all extensions except
*.abc
are valid file type extension in Windows OS. When you setAddExtension
property intrue
, only if you put a unregistered file extension, .NET Framework autocomplete automatically the file name with value of selected file extension in the save file dialog.In this example:
*.abc
(We can view file type extensions underHKEY_CLASSES_ROOT
usingregedit.exe
)*.abc
file type extension inHKEY_CLASSES_ROOT
only creating a new key with name.abc
txt
part is not visibleTo fix this, we can create an extension method that he makes sure to add the selected extension in the save file dialog
Instead to use
FileName
we can useFileNameForceExtension
. In my case, I use it that form:And this is the result using
test7.xml
with*.txt
file extension:NOTES
In the implementation of
FileDialog
of Windows Forms (FileDialog.cs on GitHub) inside the code not specified to find the file extensions using OS functions or methods,GetExtension
andHasExtension
methods only validate the pattern.<extension>
at last of the file name (Path.cs on GitHub). Maybe the validation of the registered extensions in the Windows OS is an internal functionality of the Framework and this is not visible for the developer... :(