Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a FolderBrowserDialog at the selected folder?

Tags:

.net

winforms

I have a FolderBrowserDialog, displayed with code shown below. However, it keeps opening with 'Computer', i.e. the root of the folder tree, selected. How do I get it to open on the selected folder?

       var folderBrowser = new FolderBrowserDialog();         folderBrowser.Description = "Select Chase 6 Installation Folder";         folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;         folderBrowser.ShowNewFolderButton = false;         if (Directory.Exists(Properties.Settings.Default.defaultChasePath))         {             string x = Properties.Settings.Default.defaultChasePath;             folderBrowser.SelectedPath = x;         }         if (folderBrowser.ShowDialog(this) == DialogResult.OK)         {             chasePathtext.Text = folderBrowser.SelectedPath;         } 
like image 522
ProfK Avatar asked Apr 01 '09 13:04

ProfK


People also ask

How to browse path in c#?

Once the ShowDialog method is called, you can browse and select a file. SelectedPath property represents the selected path in a FolderBrowserDialog control. RootFolder property represents the root folder from where the browsing starts.


2 Answers

If you set RootFolder to Environment.SpecialFolder.Desktop then it will open to the SelectedFolder as long as the path is valid.

When you set RootFolder to Environment.SpecialFolder.MyComputer, then the first time the dialog opens, it will always start at MyComputer, not the SelectedFolder path.

If a valid selection is made, then subsequent uses of the same FolderBrowserDialog instance will open at the previously selected path.

like image 96
Preston McCormick Avatar answered Sep 27 '22 20:09

Preston McCormick


From the Microsoft help for FolderBrowserDialog class:

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.

Are you possibly setting the SelectedPath to a location that doesn't equate to a subfolder of RootFolder (i.e. My Computer)? That would probably cause it to dive back to the RootFolder as the presented location.

like image 44
Lazarus Avatar answered Sep 27 '22 21:09

Lazarus