SaveFileDialog extension added unexpectedly on multidotExtension

1k views Asked by At

I have a problem with the save file dialog. The save file dialog looks fine until I change the save as type in the file dialog. It always add the extension that already exists. I need to have the multidot extenstion.

So, if I change the save as type, the save file dialog will make the filename become D:\Temp\Test.dt.dt.dt.dt.dt.dt.txt How to make the file the .dt not added when I switch the save as type

Is this a windows bug ? I am using winform and .net3.5 Here is how I reproduce it:

public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent();
  }

  private void button1_Click( object sender, EventArgs e )
  {
    SaveFileDialog saveFileDialog1 = new SaveFileDialog
                             {
                               Title = "Save list file",
                               Filter =  "Text Files (*.dt.txt)|*.dt.txt|Microsoft Excel Files (*.dt.xls)|*.dt.xls|Microsoft Excel XML Files (*.dt.xlsx)|*.dt.xlsx",
                               DefaultExt = ".dt.txt",
                               OverwritePrompt = true,
                               SupportMultiDottedExtensions = true,
                               AddExtension = true
                             };
    saveFileDialog1.FileName = "D:\\Temp\\test.dt.txt";
    saveFileDialog1.ShowDialog();
  }
}
2

There are 2 answers

0
Hamid Pourjam On BEST ANSWER

I think the problem is in FileDialog.cs:line877. It calls string currentExtension = Path.GetExtension(fileName); to get current extension of selected file and here is the code for Path.GetExtension(fileName);

// Returns the extension of the given path. The returned value includes the
// period (".") character of the extension except when you have a terminal period when you get String.Empty, such as ".exe" or
// ".cpp". The returned value is null if the given path is
// null or if the given path does not include an extension.
//
[Pure]
public static String GetExtension(String path) {
    if (path==null)
        return null;

    CheckInvalidPathChars(path);
    int length = path.Length;
    for (int i = length; --i >= 0;) {
        char ch = path[i];
        if (ch == '.')
        {
            if (i != length - 1)
                return path.Substring(i, length - i);
            else
                return String.Empty;
        }
        if (ch == DirectorySeparatorChar || ch == AltDirectorySeparatorChar || ch == VolumeSeparatorChar)
            break;
    }
    return String.Empty;
}

that does not support multi dotted extension. so I think this is a bug.

1
Shaharyar On

You can't add double extension

The file extension is supposed to be after the last . in the file's name.

Explanation:

Your extension is .dt.txt:

In this case, the extension will be .txt. It will consider .dt as the part of the file name(this is the reason of repetitive .dt) and .txt as the extension.