设置文件夹浏览器对话框的开始位置


113

有什么方法可以将文件夹浏览器对话框的初始目录设置为非特殊文件夹?这是我目前正在使用的

fdbLocation.RootFolder = Environment.SpecialFolder.Desktop;
但我想使用我存储在字符串中的路径,像这样
fdbLocation.RootFolder = myFolder;
这将导致错误“无法将'string'转换为'System.Environment.SpecialFolder'”。

Answers:


188

只需在调用ShowDialog之前设置SelectedPath属性即可。

fdbLocation.SelectedPath = myFolder;

20
请注意,必须将其设置RootFolder为,Environment.SpecialFolder.Desktop否则可能无法正常工作。
Mike Lowery 2014年

3
请参阅下面的Chad Grants答案:他正确地解释了必须设置RootFolder,并且SelectedPath必须位于该RootFolder 下方才能正常工作。
Snooze博士

3
这对我有用,但没有将焦点设置到文件夹。我必须手动向下滚动并找到默认文件夹。有没有办法让它在显示时自动设置焦点?
JoBaxter

2
但是,这是一样的设定RootFolder。如果RootFolder设置为,则仅指定的文件夹及其下的任何子文件夹将出现在对话框中。SelectedPath仅预选择给定的路径。
Jan Gassen '18

30

在调用ShowDialog之前,请设置SelectedPath属性。

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

将在C:\ Temp中启动它们


是否需要设置RootFolderSelectedPath is set to an absolute path that is a subfolder of RootFolder)?行为不变Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返回C:\ Users \ Myusername \ Desktop。使用模拟代码(带有LogonType LOGON32_LOGON_INTERACTIVE)返回空字符串
Kiquenet

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

“如果在显示对话框之前设置了SelectedPath属性,则只要将SelectedPath设置为RootFolder的子文件夹(或更准确地说,指向RootFolder的子文件夹)的绝对路径,具有该路径的文件夹将成为选定的文件夹。 RootFolder代表的外壳名称空间)。”

MSDN-SelectedPath

“ GetFolderPath方法返回与此枚举关联的位置。这些文件夹的位置在不同的操作系统上可以具有不同的值,用户可以更改某些位置,并且这些位置已本地化。”

回复:桌面与桌面目录

桌面

“逻辑桌面而不是物理文件系统位置。”

桌面目录:

“用于在桌面上物理存储文件对象的目录。请勿将此目录与桌面文件夹本身(虚拟文件夹)混淆。”

MSDN-特殊文件夹枚举

MSDN-GetFolderPath


对于特殊路径,您可以执行{{fldrDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory}}
tymtam 2013年

完善。谢谢。关键是,如果对话框在打开时要指向SelectedPath,则SelectedPath必须在RootFolder下方。
Snooze博士

行为不变Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返回C:\ Users \ Myusername \ Desktop。使用模拟代码(带有LogonType LOGON32_LOGON_INTERACTIVE)返回空字符串
Kiquenet

9

设置目录选择路径并检索新目录:

dlgBrowseForLogDirectory.SelectedPath = m_LogDirectory;
if (dlgBrowseForLogDirectory.ShowDialog() == DialogResult.OK)
{
     txtLogDirectory.Text = dlgBrowseForLogDirectory.SelectedPath;
}

2

dotnet-snippets.de找到

通过反射,它可以工作并设置真正的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

    }
}

知道如何扩展和折叠预设文件夹项目吗?
东西

我赞成这个答案,但是!请注意,根据msdn:docs.microsoft.com/zh-cn/windows/win32/shell/…,用户将无法浏览到高于此根文件夹设置的浏览。我使用的解决方法很简单,使用默认的.net FolderBrowser,将特殊文件夹设置为MyComputer,然后设置所选路径。这也会将文件夹扩展到所选的路径目录,但不会滚动到该目录。
Heriberto Lugo

0

就我而言,这是一次偶然的两次转义。

这有效:

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

这不是:

SelectedPath = @"C:\\Program Files\\My Company\\My product";
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.