C# 设置文件夹浏览器对话框开始位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/844423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set folder browser dialog start location
提问by Tester101
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 = Environment.SpecialFolder.Desktop;
但我想使用我存储在字符串中的路径,就像这样fdbLocation.RootFolder = myFolder;
这会导致错误“无法将‘字符串’转换为‘System.Environment.SpecialFolder’”。采纳答案by great_llama
Just set the SelectedPath property before calling ShowDialog.
只需在调用 ShowDialog 之前设置 SelectedPath 属性。
fdbLocation.SelectedPath = myFolder;
回答by Chad Grant
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)."
"如果在显示对话框之前设置了 SelectedPath 属性,则具有此路径的文件夹将是所选文件夹,只要将 SelectedPath 设置为绝对路径即 RootFolder 的子文件夹(或者更准确地说,指向 RootFolder 的子文件夹) RootFolder 表示的 shell 命名空间)。”
"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."
“GetFolderPath 方法返回与此枚举关联的位置。这些文件夹的位置在不同的操作系统上可以有不同的值,用户可以更改某些位置,并且这些位置是本地化的。”
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."
“用于在桌面上物理存储文件对象的目录。不要将此目录与桌面文件夹本身混淆,后者是一个虚拟文件夹。”
回答by JP Alioto
Set the SelectedPath property before you call ShowDialog ...
在调用 ShowDialog 之前设置 SelectedPath 属性...
folderBrowserDialog1.SelectedPath = @"c:\temp\";
folderBrowserDialog1.ShowDialog();
Will start them at C:\Temp
将在 C:\Temp 启动它们
回答by Jim Lahman
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;
}
回答by Andy U.
Found on dotnet-snippets.de
With reflection this works and sets the real RootFolder!
通过反射,这可以工作并设置真正的 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
}
}
回答by Flot2011
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";