Answers:
只需在调用ShowDialog之前设置SelectedPath属性即可。
fdbLocation.SelectedPath = myFolder;
RootFolder。如果RootFolder设置为,则仅指定的文件夹及其下的任何子文件夹将出现在对话框中。SelectedPath仅预选择给定的路径。
在调用ShowDialog之前,请设置SelectedPath属性。
folderBrowserDialog1.SelectedPath = @"c:\temp\";
folderBrowserDialog1.ShowDialog();
将在C:\ Temp中启动它们
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)返回空字符串
fldrDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
“如果在显示对话框之前设置了SelectedPath属性,则只要将SelectedPath设置为RootFolder的子文件夹(或更准确地说,指向RootFolder的子文件夹)的绝对路径,具有该路径的文件夹将成为选定的文件夹。 RootFolder代表的外壳名称空间)。”
“ GetFolderPath方法返回与此枚举关联的位置。这些文件夹的位置在不同的操作系统上可以具有不同的值,用户可以更改某些位置,并且这些位置已本地化。”
回复:桌面与桌面目录
桌面
“逻辑桌面而不是物理文件系统位置。”
桌面目录:
“用于在桌面上物理存储文件对象的目录。请勿将此目录与桌面文件夹本身(虚拟文件夹)混淆。”
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返回C:\ Users \ Myusername \ Desktop。使用模拟代码(带有LogonType LOGON32_LOGON_INTERACTIVE)返回空字符串
通过反射,它可以工作并设置真正的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
}
}
RootFolder为,Environment.SpecialFolder.Desktop否则可能无法正常工作。