2

I'm using CommonSaveFileDialog of WindowsAPICodePack in WPF to ask the user for a filename. In order to check the filename before the dialog is closed, I have a handler for FileOk. But if I try to get the FileName property, there is an InvalidOperationException thrown saying that this property is not available before the dialog is closed.

So how can I retrieve the full file name that the user selected or entered, when in FileOk handler?

MTR
  • 1,690
  • 3
  • 20
  • 47
  • 1
    Yes, that's a flaw in the code. It is technically fixable by calling PopulateFileNames in OnFileOk and changing CheckFileNamesAvailable. – Hans Passant Apr 11 '12 at 10:32
  • Ok, so if there is no other possibility to get the file name, I will workaround this problem by checking after the dialog is closed. – MTR Apr 12 '12 at 14:04

1 Answers1

1

A possible workaround as suggested by Hans Passant without modifying source code :

dialog.FileOk += (sender, e) =>
{
    var filenames = new Collection<string>();
    typeof(CommonFileDialog)
        .GetMethod("PopulateWithFileNames", BindingFlags.Instance | BindingFlags.NonPublic)
        .Invoke(dialog, new[] { filenames });

    // use filenames
}
McX
  • 1,296
  • 2
  • 12
  • 16