回答:
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)を設定する必要がありますか?そのままの動作:C:\ Users \ Myusername \ DesktopをEnvironment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返します。偽装コードを使用すると(LogonType LOGON32_LOGON_INTERACTIVEで)空の文字列
fldrDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
「ダイアログボックスを表示する前にSelectedPathプロパティを設定した場合、SelectedPathがRootFolderのサブフォルダーである絶対パスに設定されている(より正確には、 RootFolderによって表されるシェルの名前空間)。」
「GetFolderPathメソッドは、この列挙に関連付けられた場所を返します。これらのフォルダーの場所は、異なるオペレーティングシステムでは異なる値を持つことができ、ユーザーは一部の場所を変更でき、場所はローカライズされています。」
Re:デスクトップとDesktopDirectory
デスクトップ
「物理的なファイルシステムの場所ではなく、論理的なデスクトップ。」
DesktopDirectory:
「デスクトップにファイルオブジェクトを物理的に保存するために使用されるディレクトリ。このディレクトリを仮想フォルダであるデスクトップフォルダ自体と混同しないでください。」
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返します。偽装コード(LogonType LOGON32_LOGON_INTERACTIVE を使用)を使用すると、空の文字列
に見つかりました 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
}
}
RootFolderする必要があることに注意してくださいEnvironment.SpecialFolder.Desktop。そうしないと機能しない場合があります。