在 multidotExtension 上意外添加了 SaveFileDialog 扩展

SaveFileDialog extension added unexpectedly on multidotExtension

保存文件对话框有问题。在我更改文件对话框中的 save as type 之前,保存文件对话框看起来很好。它总是添加已经存在的扩展名。我需要多点扩展名。

因此,如果我更改 save as type,保存文件对话框将使文件名变为 D:\Temp\Test.dt.dt.dt.dt.dt.dt.txt 当我切换 save as type

时,如何使文件 .dt 不添加

这是 windows 错误吗?我正在使用 winform 和 .net3.5 以下是我如何重现它:

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();
  }
}

您不能添加双重扩展

文件扩展名应该在文件名最后一个 . 之后。

解释:

您的分机是 .dt.txt:

在这种情况下,扩展名为 .txt。它会将 .dt 视为文件名的一部分(这是重复 .dt 的原因),将 .txt 视为扩展名。

我认为问题出在FileDialog.cs:line877。 它调用 string currentExtension = Path.GetExtension(fileName); 来获取所选文件的当前扩展名,这里是 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;
}

不支持多点扩展。所以我认为这是一个错误。