The other day I had the following question SaveFileDialog AddExtension doesn't work as expected. Now a follow up question came to my mind.
Should I use the FilterIndex property or the extension of the FileName property of the SaveFileDialog to decide under which file format I want to store the data?
I've the following C# test code:
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)
{
string extension = System.IO.Path.GetExtension(dialog.FileName);
int filterIndex = dialog.FilterIndex;
}
The documentation quotes that:
You can also use the value of FilterIndex after showing the file dialog to perform special file operations depending upon the filter chosen.
If I use the FilterIndex
property it will save for example a text document with an XML extension (Dialog File name = test7.xml
, Dialog Save as type = *.txt
).
If I use the extension of the FileName
then the Save as type
of the dialog is ignored.