3

如何使用 FolderBrowserDialog 选择多个文件夹?

目前,我一次只能使用以下代码浏览一个文件夹。

FolderBrowserDialog dialog;
private void btnbrowse_Click(object sender, EventArgs e)
    {
        dialog = new FolderBrowserDialog();

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            txtFolderPath.Text = dialog.SelectedPath;

        }
    }

如何浏览多个文件夹?

4

5 回答 5

4

但是您可以使用 System.Windows.Forms.OpenFileDialog() 来实现它。它允许您选择多个文件夹

请看以下示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FolderDialog
{
    public class FolderSelectDialog
    {
        System.Windows.Forms.OpenFileDialog ofd = null;
        public FolderSelectDialog()
        {
            ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Folders|\n";
            ofd.AddExtension = false;
            ofd.CheckFileExists = false;
            ofd.DereferenceLinks = true;
            ofd.Multiselect = Multiselect;    

        }
        public string InitialDirectory
        {
            get { return ofd.InitialDirectory; }
            set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
        }
        public string Title
        {
            get { return ofd.Title; }
            set { ofd.Title = value == null ? "Select a folder" : value; }
        }

        public bool Multiselect
        {
            get { return ofd.Multiselect; }
            set { ofd.Multiselect = value == false ? false : value; }
        }

        public string FileName
        {
            get { return ofd.FileName; }
        }

        public string[] FileNames
        {
            get { return ofd.FileNames; }
        }
        public bool ShowDialog()
        {
            return ShowDialog(IntPtr.Zero);
        }
        public bool ShowDialog(IntPtr hWndOwner)
        {
            bool flag = false;

            if (Environment.OSVersion.Version.Major >= 6)
            {
                var r = new Reflector("System.Windows.Forms");
                uint num = 0;
                Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
                object dialog = r.Call(ofd, "CreateVistaDialog");
                r.Call(ofd, "OnBeforeVistaDialog", dialog);
                uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
                options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
                r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
                object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
                object[] parameters = new object[] { pfde, num };
                r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
                num = (uint)parameters[1];
                try
                {
                    int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
                    flag = 0 == num2;
                }
                finally
                {
                    r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
                    GC.KeepAlive(pfde);
                }
            }
            else
            {
                var fbd = new FolderBrowserDialog();
                fbd.Description = this.Title;
                fbd.SelectedPath = this.InitialDirectory;
                fbd.ShowNewFolderButton = false;
                if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
                ofd.FileName = fbd.SelectedPath;
                flag = true;
            }
            return flag;
        }

        public class WindowWrapper : System.Windows.Forms.IWin32Window
        {
            public WindowWrapper(IntPtr handle)
            {
                _hwnd = handle;
            }
            public IntPtr Handle
            {
                get { return _hwnd; }
            }

            private IntPtr _hwnd;
        }

        public class Reflector
        {
            string m_ns;
            Assembly m_asmb;
            public Reflector(string ns)
                : this(ns, ns)
            { }
            public Reflector(string an, string ns)
            {
                m_ns = ns;
                m_asmb = null;
                foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
                {
                    if (aN.FullName.StartsWith(an))
                    {
                        m_asmb = Assembly.Load(aN);
                        break;
                    }
                }
            }
            public Type GetType(string typeName)
            {
                Type type = null;
                string[] names = typeName.Split('.');

                if (names.Length > 0)
                    type = m_asmb.GetType(m_ns + "." + names[0]);

                for (int i = 1; i < names.Length; ++i)
                {
                    type = type.GetNestedType(names[i], BindingFlags.NonPublic);
                }
                return type;
            }
            public object New(string name, params object[] parameters)
            {
                Type type = GetType(name);
                ConstructorInfo[] ctorInfos = type.GetConstructors();
                foreach (ConstructorInfo ci in ctorInfos)
                {
                    try
                    {
                        return ci.Invoke(parameters);
                    }
                    catch { }
                }

                return null;
            }
            public object Call(object obj, string func, params object[] parameters)
            {
                return Call2(obj, func, parameters);
            }
            public object Call2(object obj, string func, object[] parameters)
            {
                return CallAs2(obj.GetType(), obj, func, parameters);
            }
            public object CallAs(Type type, object obj, string func, params object[] parameters)
            {
                return CallAs2(type, obj, func, parameters);
            }
            public object CallAs2(Type type, object obj, string func, object[] parameters)
            {
                MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                return methInfo.Invoke(obj, parameters);
            }
            public object Get(object obj, string prop)
            {
                return GetAs(obj.GetType(), obj, prop);
            }
            public object GetAs(Type type, object obj, string prop)
            {
                PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                return propInfo.GetValue(obj, null);
            }
            public object GetEnum(string typeName, string name)
            {
                Type type = GetType(typeName);
                FieldInfo fieldInfo = type.GetField(name);
                return fieldInfo.GetValue(null);
            }
        }
    }
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }


        public static string OpenFileBrowserDialog(bool multiselect) {
            FolderSelectDialog fbd = new FolderSelectDialog();
            fbd.Multiselect = multiselect;

            fbd.ShowDialog();

            string selected_folders = "";
            if (multiselect)
            {
                string[] names = fbd.FileNames;
                selected_folders = string.Join(",", names);
            }
            else
            {
                selected_folders = fbd.FileName;
            }

            return selected_folders;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string name = OpenFileBrowserDialog(true);

            MessageBox.Show(name);
            Environment.Exit(0);
        }
    }
}
于 2018-02-16T10:25:08.527 回答
2

没有对多文件夹选择的原生支持FolderBrowserDialog,但您可以结合树视图和“多选”构建自己的对话框。

看一看:

http://www.codeproject.com/Articles/13375/File-Explorer-using-Treeview-controller-in-C-2-0

http://www.arstdesign.com/articles/treeviewms.html

于 2012-09-04T10:40:56.920 回答
1

默认的 FolderBrowserDialog 不允许您选择多个文件夹。

您可以考虑使用像FolderView这样的第 3 方控件- 它还带有一个使用 FolderView 的即用型自定义 FolderBrowserDialog。

免责声明:我为 FolderView 的开发商 LogicNP Software 工作。

于 2012-09-05T05:41:12.560 回答
0

不幸的是,你不能这样做FolderBrowserDialog

于 2012-09-04T10:34:33.740 回答
0

您可以结帐BetterFolderBrowser。它是一个 .NET 库,可以满足您的需要以及更多功能。同样,它提供了一种更易于使用的模仿OpenFileDialog对话窗口的文件夹浏览体验。希望能帮助到你。

于 2020-05-24T17:20:27.830 回答