フォルダブラウザダイアログの開始場所を設定


113

フォルダーブラウザーダイアログの初期ディレクトリを特別でないフォルダーに設定する方法はありますか?これは私が現在使用しているものです

fdbLocation.RootFolder = Environment.SpecialFolder.Desktop;
このような文字列に保存したパスを使用したい
fdbLocation.RootFolder = myFolder;
これにより、「 'string'を 'System.Environment.SpecialFolder'に変換できません」というエラーが発生します。

回答:


188

ShowDialogを呼び出す前にSelectedPathプロパティを設定するだけです。

fdbLocation.SelectedPath = myFolder;

20
に設定RootFolderする必要があることに注意してくださいEnvironment.SpecialFolder.Desktop。そうしないと機能しない場合があります。
Mike Lowery 2014年

3
以下のChad Grantsの回答を参照してください。彼は、RootFolderを設定する必要があり、SelectedPathがそのRootFolderの下にある必要があると正しく説明しています。
Dr Snooze、2015年

3
これは私にとっては機能しますが、フォルダにフォーカスを設定しません。手動で下にスクロールして、デフォルトのフォルダを見つける必要があります。表示されたときに自動的にフォーカスを設定する方法はありますか?
JoBaxter 2016

2
ただし、これは設定と同じではありませんRootFolderRootFolderが設定されている場合、指定したフォルダとその下のサブフォルダのみがダイアログボックスに表示されます。SelectedPath指定されたパスを単に事前選択するだけです。
Jan Gassen

30

ShowDialogを呼び出す前にSelectedPathプロパティを設定します...

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

C:\ Tempから開始します


RootFolderSelectedPath 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で)空の文字列
Kiquenet

24
fldrDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

「ダイアログボックスを表示する前にSelectedPathプロパティを設定した場合、SelectedPathがRootFolderのサブフォルダーである絶対パスに設定されている(より正確には、 RootFolderによって表されるシェルの名前空間)。」

MSDN-SelectedPath

「GetFolderPathメソッドは、この列挙に関連付けられた場所を返します。これらのフォルダーの場所は、異なるオペレーティングシステムでは異なる値を持つことができ、ユーザーは一部の場所を変更でき、場所はローカライズされています。」

Re:デスクトップとDesktopDirectory

デスクトップ

「物理的なファイルシステムの場所ではなく、論理的なデスクトップ。」

DesktopDirectory:

「デスクトップにファイルオブジェクトを物理的に保存するために使用されるディレクトリ。このディレクトリを仮想フォルダであるデスクトップフォルダ自体と混同しないでください。」

MSDN-特別なフォルダ列挙

MSDN-GetFolderPath


特別なパスについては、{{fldrDialog.RootFolder = Environment.SpecialFolder.DesktopDirectory}}を実行できます
tymtam

完璧です。ありがとうございました。重要なのは、ダイアログを開くときにSelectedPathをポイントする場合、SelectedPathがRootFolderの下にある必要があることです。
Dr Snooze、2015年

そのままの動作:C:\ Users \ Myusername \ DesktopをEnvironment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)返します偽装コード(LogonType LOGON32_LOGON_INTERACTIVE を使用)を使用すると、空の文字列
Kiquenet

9

選択したディレクトリのパスを設定し、新しいディレクトリを取得するには:

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

2

に見つかりました 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

    }
}

プリセットフォルダーアイテムを展開および折りたたむ方法はありますか?
Goodies

私は賛成してこの回答が好きですが、!! msdn:docs.microsoft.com/en-us/windows/win32/shell/…によると、ユーザーはこのルートフォルダーに設定されているものより上位を参照できないことに注意してください。私が使用した回避策は簡単です。デフォルトの.net FolderBrowserを使用し、特別なフォルダーをMyComputerに設定してから、選択したパスを設定します。これにより、選択したパスディレクトリまでフォルダーが展開されますが、スクロールされません。
Heriberto Lugo

0

私の場合、それは偶発的な二重脱出でした。

これは機能します:

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

これはしません:

SelectedPath = @"C:\\Program Files\\My Company\\My product";
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.