2

This is a WPF app with the latest version of the .NET framework and VS2015 on a Win 10 box.

I am trying to use the "CommonOpenFileDialog" from the Windows API code pack 1.1 to allow the user to establish a folder in which to do some stuff. The folder can be either an existing folder, or a new one that the user specifies.

If the user wants to create a new folder, then I want them to be able to specify the folder by editing the text within the "Folder:" textbox at the bottom of the dialog. Within this context, the dialog would just be a means by which to navigate to the folder in which the new one is to be created. My plan is to validate the input within my code to check for a valid (existing) path, and simply create the path if it does not exist.

Here is the code:

   private void test1_folderSelectorDialog ()
   {
       if (CommonFileDialog.IsPlatformSupported)
       {
            var folderSelectorDialog = new CommonOpenFileDialog();
            folderSelectorDialog.EnsureReadOnly = false;
            folderSelectorDialog.IsFolderPicker = true;
            folderSelectorDialog.Multiselect = false;
            folderSelectorDialog.EnsureValidNames = false;
            folderSelectorDialog.EnsurePathExists = false;
            folderSelectorDialog.EnsureFileExists = false;

            folderSelectorDialog.InitialDirectory
                 = "C:\\My_Initial_Directory";

            folderSelectorDialog.Title = "test1_folderSelectorDialog";

            if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
                TxBx_folder.Text = folderSelectorDialog.FileName;

            this.Focus();
       }
       else
           MessageBox.Show ("CommonFileDialog is not supported");
   }

When I run the dialog and modify the text within the "Folder:" textbox, then press "Select Folder", the dialog validates the input and issues a dialog popup with the message:

"Path does not exist. Check the path and try again."

Please note that I have set "EnsureValidNames", "EnsurePathExists", and "EnsureFileExists" to "false". (If they do not control dialog validation, then what are they there for?)

I can right-click on the dialog window and use "new > Folder" to create a new folder (which is what I'll have to do if I cannot resolve this issue), but I'd rather do it the way that I am trying to do it, as it seems much easier and more intuitive to do it that way.

How do I get the silly thing to shaddup and just accept the input without passing judgement upon it?

Thanks!

Wally Walrus
  • 253
  • 1
  • 2
  • 7

1 Answers1

0

If you want the user to select only the folder, then below code is enough

CommonOpenFileDialog dialog = new CommonOpenFileDialog()
dialog.IsFolderPicker = true
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
    filesPath = dialog.FileName
}

I believe below things are not required

folderSelectorDialog.Multiselect = false
folderSelectorDialog.EnsureValidNames = false
folderSelectorDialog.EnsurePathExists = false
folderSelectorDialog.EnsureFileExists = false
Slavcho
  • 2,792
  • 3
  • 30
  • 48
vinaya
  • 1