【问题标题】:Setting the root folder of folder browser dialog设置文件夹浏览器对话框的根文件夹
【发布时间】:2014-05-26 09:11:14
【问题描述】:

您好,我正在尝试设置文件夹浏览器对话框的根文件夹。 FolderBrowserDialog 的 RootFolder 属性可以设置为 Environment.SpecialFolder 枚举的元素。

FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.RootFolder = Environment.SpecialFolder.MyDocuments;
if (folderDlg.ShowDialog() == DialogResult.OK) {
...
}

问题是我不想将根文件夹设置为默认值。我希望它取决于一些用户选择。

我已经阅读了有关在显示对话框之前设置 selectedPath 属性的信息。因此我尝试了这段代码

folderDlg.SelectedPath = pathSelectedByUser;

其中 pathSelectedByUser 是类似“C:\Temp\Backup”的字符串。差不多就是这样,但是只打开了Temp目录,选择了Backup目录,并没有打开Backup目录。

谁能帮忙?

【问题讨论】:

  • 你希望如何让用户选择默认路径?
  • 试试pathSelectedByUser = "C:\Temp\Backup\"
  • 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).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.

标签: c# winforms


【解决方案1】:

在显示对话框之前设置SelectedPath 将强制突出显示所选路径并展开文件树以显示所选级别文件夹。不幸的是,滚动不会导航到选择(将其集中在窗口中)。

如您所说,不会打开所选路径(展开以显示子文件夹),但有一种解决方法。如果您按“+”号,它将完成这项工作,因此可以使用SendKeys.SendWait,如下所述:

Why FolderBrowserDialog dialog does not scroll to selected folder?

问候,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多