0

Hello I'm trying to set the root folder of a folder browser dialog. The RootFolder property of the FolderBrowserDialog can be set to an element of Environment.SpecialFolder enum.

FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.RootFolder = Environment.SpecialFolder.MyDocuments;
if (folderDlg.ShowDialog() == DialogResult.OK) {
...
}

Problem is i dont want to set the root folder to some default. I want it to depend on some user selection.

Ive read about setting the selectedPath property before the dialog is shown. Therefore i tried this code

folderDlg.SelectedPath = pathSelectedByUser;

where pathSelectedByUser is a string like "C:\Temp\Backup". This is almost doing it, but it only opens the Temp directory and selects the Backup directory and doesnt open the Backup directory.

Can anyone help?

user3596113
  • 868
  • 14
  • 32
  • How do you want to allow the user to select the defaultpath? – Matthijs May 26 '14 at 09:13
  • try with `pathSelectedByUser = "C:\Temp\Backup\"` – Milen May 26 '14 at 09:13
  • http://stackoverflow.com/questions/12946488/is-it-possible-to-use-non-special-folder-as-a-folderbrowserdialogs-root-folder, http://stackoverflow.com/questions/844423/set-folder-browser-dialog-start-location – CodeCaster May 26 '14 at 09:16
  • `If the SelectedPath property is set before showing the dialog box, the folder with this path will be the selected folder, as long as SelectedPath is set to an absolute path that is a subfolder of RootFolder (or more accurately, points to a subfolder of the shell namespace represented by RootFolder).`. `Typically, after creating a new FolderBrowserDialog, you set the RootFolder to the location from which to start browsing. Optionally, you can set the SelectedPath to an absolute path of a subfolder of RootFolder that will initially be selected.` – Kiquenet Mar 16 '17 at 12:49

1 Answers1

0

Setting the SelectedPath before showing the dialog will force the selected path to be highlighted and the file tree expanded to show the selected level folder. Unfortunate the scroll will not navigate till the selection (centralizing it in the window would be expected).

As you said, the selected path will not be opened (expanded to show subfolders), but there is a workaround. If you press '+' sign it will do the job, so a SendKeys.SendWait might be used as explained in:

Why FolderBrowserDialog dialog does not scroll to selected folder?

Regards,