Set folder browser dialog start location

后端 未结 6 506
长情又很酷
长情又很酷 2020-12-24 11:02

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 = Envir         


        
相关标签:
6条回答
  • 2020-12-24 11:42

    Set the SelectedPath property before you call ShowDialog ...

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

    Will start them at C:\Temp

    0 讨论(0)
  • 2020-12-24 11:42
    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

    0 讨论(0)
  • 2020-12-24 11:44

    To set the directory selected path and the retrieve the new directory:

    dlgBrowseForLogDirectory.SelectedPath = m_LogDirectory;
    if (dlgBrowseForLogDirectory.ShowDialog() == DialogResult.OK)
    {
         txtLogDirectory.Text = dlgBrowseForLogDirectory.SelectedPath;
    }
    
    0 讨论(0)
  • 2020-12-24 11:46

    Just set the SelectedPath property before calling ShowDialog.

    fdbLocation.SelectedPath = myFolder;
    
    0 讨论(0)
  • 2020-12-24 11:46

    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";
    
    0 讨论(0)
  • 2020-12-24 11:55

    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
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题