SaveFileDialog AddExtension doesn't work as expected

早过忘川 提交于 2019-12-04 15:20:40

Source code on my Github (batressc)

In simple terms, all extensions except *.abc are valid file type extension in Windows OS. When you set AddExtension property in true, 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:

  1. In my Windows 10 OS, I don't have registered the file type extension *.abc (We can view file type extensions under HKEY_CLASSES_ROOT using regedit.exe)

  1. I test "test3.abc" case with the expected result

  1. I register the *.abc file type extension in HKEY_CLASSES_ROOT only creating a new key with name .abc

  1. I repeat point 2 and now the txt part is not visible

To fix this, we can create an extension method that he makes sure to add the selected extension in the save file dialog

// It's good practice create extensions methods in the same namespace of the class to extend
namespace System.Windows.Forms {
    public static class SaveFileDialogFileTypeExtension {
        // Retrieving only text of the file extensions
        private static List<string> GetFileExtensions(string filter) {
            List<string> extensions = new List<string>();
            var filtersRaw = filter.Split('|');
            for (int i = 0; i < filtersRaw.Length; i++) {
                if (i % 2 != 0) {
                    // Supporting multi doted extensions
                    extensions.Add(filtersRaw[i].Trim().Replace("*", "").Substring(1));
                }
            }
            return extensions;
        }

        // Getting filename with selected extension
        public static string FileNameForceExtension(this SaveFileDialog dialog) {
            string fileName = dialog.FileName;
            // Retrieving the current selected filter index
            List<string> extensions = GetFileExtensions(dialog.Filter);
            string selectedExtension = extensions[dialog.FilterIndex - 1];
            // Adding extension if need it
            if (!fileName.EndsWith($".{selectedExtension}")) {
                fileName = $"{fileName}.{selectedExtension}";
            }
            return fileName;
        }
    }
}

Instead to use FileName we can use FileNameForceExtension. In my case, I use it that form:

textBoxFileName.Text = dialog.FileName + " | " + dialog.FileNameForceExtension();

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 and HasExtension 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... :(

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!