0

I am trying to save a file with a double-dot extension. But whenever I do that the file ends up with the extension doubled in my case I choose to save as .lmk.ilu extension but then the file ends up as filename.lmk.ilu.lmk.ilu

Here`s the code:

public static bool BrowseForFileToSave(BrowserInfo info, IWin32Window owner, string title)
    {
        var dlg = new SaveFileDialog
        {
            OverwritePrompt = info.OverwritePrompt,
            DefaultExt = info.DefaultExt,
            Filter = info.Filter,
            FilterIndex = info.FilterIndex,
            FileName = info.FileName,
            InitialDirectory =
                info.UseLastFolderAsInitialDirectory && BrowserInfo.LastFolder != null
                    ? BrowserInfo.LastFolder
                    : info.InitialDirectory
        };

        if (title != null)
            dlg.Title = title;

        if(info.FileNameValidationCallback != null)
            dlg.FileOk += info.FileNameValidationCallback;

        var result = dlg.ShowDialog(owner);
        if (result == DialogResult.OK)
        {
            BrowserInfo.LastFolder = Path.GetDirectoryName(dlg.FileName);
            info.FileName = dlg.FileName;
            return true;
        }

        return false;
    }

Everything is fine when debugging up to

var result = dlg.ShowDialog(owner);

Up to this point dlg.FileName is null. Then after the dialog operations finish the name is already with four extensions instead of two.

How can I make it have only two extensions when I choose two extensions, given that single dot extensions work fine, and that SupportMultiDottedExtensions = true didn`t help?

Edit:

Save File Dialog , restrict name

I implemented the File.Ok name validation but it works once the "Save" button is pressed.

How can I validate extensions on selecting an option from the "Save as type" dropdown, i.e. before the dialog is closed?

starter
  • 1
  • 2
  • First time i have ever seen `SupportMultiDottedExtensions` and its been available since 2.0, i'm so out of the loop – TheGeneral Aug 07 '18 at 09:25
  • What is your filter? and DefaultExt – TheGeneral Aug 07 '18 at 09:32
  • The part of the filter for this extension is |Landmark|*.lmk.ilu| ; DefaultExt is null – starter Aug 07 '18 at 09:38
  • And that `FileNameValidationCallback` (which is an event handler)? What is it doing? The `result == DialogResult.OK` consequence should be its job. – Jimi Aug 08 '18 at 02:34
  • both FileNameValidationCallback and title are null in the cases I am testing – starter Aug 08 '18 at 07:39
  • Why is that? The `FileOK` event is there for this reason, validate the user input in a semi-transparent fashion. Subscribe it in-line, Replace any occurence of ".lmk.ilu" and add one back to the SFD FileName. Note that if you are using the `SaveFileDialog` class (not the ToolBox object). The `FileOK` event is hidden, but you can subscribe it anyway. It's hidden, but still there. You could also register your file extension. – Jimi Aug 08 '18 at 11:41
  • Could you elaborate please what do you mean by semi-transparent fasion, subscribe and register? At first I tried just trimming the resulting FileName string after the dialog operations were finished but I want to solve the cause of the issue, not the effect. – starter Aug 08 '18 at 13:34
  • Semi-transparent: managed validation before the main-flow code reads the FileName. Subscribe: `Control.Event += (obj, event) => { [Code] };`. Register: [Associate File Extension with Application](https://stackoverflow.com/questions/2681878/associate-file-extension-with-application). – Jimi Aug 08 '18 at 21:00
  • Registering is not viable since many PCs will use the dialog. I implemented FileNameValidationCallback but it only works when pressing the Save button, is there any way to execute a validation upon selecting an option from the "Save as type" dropdown? – starter Oct 02 '18 at 13:34

0 Answers0