Set folder browser dialog start location

Viewed 177002

Is there any way to set the initial directory of a folder browser dialog to a non-special folder? This is what I'm currently using

fdbLocation.RootFolder = Environment.SpecialFolder.Desktop;
but I want to use a path I have stored in a string something like this
fdbLocation.RootFolder = myFolder;
This causes an error "Cannot convert 'string' to 'System.Environment.SpecialFolder'".

8 Answers

Just set the SelectedPath property before calling ShowDialog.

fdbLocation.SelectedPath = myFolder;

Set the SelectedPath property before you call ShowDialog ...

folderBrowserDialog1.SelectedPath = @"c:\temp\";
folderBrowserDialog1.ShowDialog();

Will start them at C:\Temp

fldrDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

"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)."

MSDN - SelectedPath

"The GetFolderPath method returns the locations associated with this enumeration. The locations of these folders can have different values on different operating systems, the user can change some of the locations, and the locations are localized."

Re: Desktop vs DesktopDirectory

Desktop

"The logical Desktop rather than the physical file system location."

DesktopDirectory:

"The directory used to physically store file objects on the desktop. Do not confuse this directory with the desktop folder itself, which is a virtual folder."

MSDN - Special Folder Enum

MSDN - GetFolderPath

Found on dotnet-snippets.de

With reflection this works and sets the real RootFolder!

using System;
using System.Reflection;
using System.Windows.Forms;

namespace YourNamespace
{
    public class RootFolderBrowserDialog
    {

        #region Public Properties

        /// <summary>
        ///   The description of the dialog.
        /// </summary>
        public string Description { get; set; } = "Chose folder...";

        /// <summary>
        ///   The ROOT path!
        /// </summary>
        public string RootPath { get; set; } = "";

        /// <summary>
        ///   The SelectedPath. Here is no initialization possible.
        /// </summary>
        public string SelectedPath { get; private set; } = "";

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        ///   Shows the dialog...
        /// </summary>
        /// <returns>OK, if the user selected a folder or Cancel, if no folder is selected.</returns>
        public DialogResult ShowDialog()
        {
            var shellType = Type.GetTypeFromProgID("Shell.Application");
            var shell = Activator.CreateInstance(shellType);
            var folder = shellType.InvokeMember(
                             "BrowseForFolder", BindingFlags.InvokeMethod, null,
                             shell, new object[] { 0, Description, 0, RootPath, });
            if (folder is null)
            {
                return DialogResult.Cancel;
            }
            else
            {
                var folderSelf = folder.GetType().InvokeMember(
                                     "Self", BindingFlags.GetProperty, null,
                                     folder, null);
                SelectedPath = folderSelf.GetType().InvokeMember(
                                   "Path", BindingFlags.GetProperty, null,
                                   folderSelf, null) as string;
                // maybe ensure that SelectedPath is set
                return DialogResult.OK;
            }
        }

        #endregion Public Methods

    }
}

In my case, it was an accidental double escaping.

this works:

SelectedPath = @"C:\Program Files\My Company\My product";

this doesn't:

SelectedPath = @"C:\\Program Files\\My Company\\My product";

It's easy and you don't need reflection. You have to set the SelectedPath property to desired folder but, because SelectedPath is ONLY set to an absolute path that is a subfolder of RootFolder before, you have to set the RootFolder. For example:

Your initial folder is a Desktop subfolder:

dlgBrowseForLogDirectory.RootFolder = Environment.SpecialFolder.DesktopDirectory;
dlgBrowseForLogDirectory.SelectedPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "yourDesktopFolder");

Your initial folder is a normal folder (so is a My Computer subfolder):

dlgBrowseForLogDirectory.RootFolder = Environment.SpecialFolder.MyComputer;
dlgBrowseForLogDirectory.SelectedPath = @"e:\yourFolder";

Have nice coding, Claudio

FOR THOSE FIGHTING WITH THIS..

There is a hidden feature that affects how the dialog behaves.

When you enter directory path without trailing \, for example c:\temp\abc, then the dialog opens in c:\temp and abc is written in Folder, so it's basically selected but you are in parent folder

When you enter directory path with trailing \, for example c:\temp\abc, then it actually opens c:\temp\abc folder so you are inside it.

dialog.RootFolder does NOT need to be set for this to work.. At least on my computer with .NET 5

Related