【问题标题】:Set folder browser dialog start location设置文件夹浏览器对话框开始位置
【发布时间】:2022-01-09 21:28:12
【问题描述】:

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

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

【问题讨论】:

    标签: c#


    【解决方案1】:

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

    fdbLocation.SelectedPath = myFolder;
    

    【讨论】:

    • 请注意,必须将RootFolder 设置为Environment.SpecialFolder.Desktop,否则可能不起作用。
    • 请参阅下面的 Chad Grants 回答:他正确地解释了必须设置 RootFolder,并且 SelectedPath 必须低于该 RootFolder 才能工作。
    • 这对我有用,但不会将焦点设置到文件夹。我必须手动向下滚动并找到它默认的文件夹。有没有办法让它在显示时自动设置焦点?
    • 这与设置RootFolder相同。如果设置了RootFolder,则只有指定的文件夹及其下的所有子文件夹会出现在对话框中。 SelectedPath 只是预先选择给定的路径。
    【解决方案2】:

    在调用ShowDialog 之前设置SelectedPath 属性...

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

    将在C:\Temp开始他们

    【讨论】:

    • 需要设置 RootFolder (SelectedPath 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 )返回 空字符串
    【解决方案3】:
    fldrDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
    

    "如果在显示对话框之前设置了SelectedPath属性,则只要将SelectedPath设置为RootFolder的子文件夹的绝对路径(或者更准确地说,指向RootFolder 表示的 shell 命名空间的子文件夹)。”

    MSDN - SelectedPath

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

    关于:桌面与桌面目录

    桌面

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

    桌面目录:

    "用于物理上的目录 在桌面上存储文件对象。做 不要将此目录与 桌面文件夹本身,这是一个 虚拟文件夹。”

    MSDN - Special Folder Enum

    MSDN - GetFolderPath

    【讨论】:

    • 对于特殊路径,您可以执行 {{fldrDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory}}
    • 完美。谢谢你。关键是,如果对话框要在打开时指向 SelectedPath,则 SelectedPath 必须位于 RootFolder 之下。
    • 行为原样:Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) 返回 C:\Users\Myusername\Desktop。 使用 模拟代码 (使用 LogonType LOGON32_LOGON_INTERACTIVE ) 返回 空字符串
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      发现于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/en-us/windows/win32/shell/…,用户将无法浏览高于此根文件夹设置的内容。我使用的解决方法很简单,使用默认的 .net FolderBrowser,将特殊文件夹设置为 MyComputer,然后设置所选路径。这也会将文件夹展开到选定的路径目录,但不会滚动到它。
      【解决方案6】:

      就我而言,这是一次意外的双重转义。

      这行得通:

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

      这不是:

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

      【讨论】:

        【解决方案7】:

        这很简单,您不需要反思。您必须将 SelectedPath 属性设置为所需的文件夹,但由于 SelectedPath 之前仅设置为绝对路径,即 RootFolder 的子文件夹,因此您必须设置 RootFolder。例如:

        您的初始文件夹是桌面子文件夹:

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

        您的初始文件夹是一个普通文件夹(我的电脑子文件夹也是如此):

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

        有很好的编码, 克劳迪奥

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-18
          相关资源
          最近更新 更多