Recherchez un répertoire dans C #

Comment puis-je présenter un contrôle à l’utilisateur lui permettant de sélectionner un répertoire?

Il ne semble pas y avoir de contrôles .net natifs qui le font?

La classe FolderBrowserDialog est la meilleure option.

ssortingng folderPath = ""; FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog(); if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { folderPath = folderBrowserDialog1.SelectedPath ; } 

Note: il n’y a aucune garantie que ce code fonctionnera dans les futures versions du framework .Net. L’utilisation d’internes de framework .Net privés comme cela est le cas ici par reflection n’est probablement pas bonne dans l’ensemble. Utilisez la solution d’interopérabilité mentionnée en bas, car l’API Windows est moins susceptible de changer.

Si vous recherchez un sélecteur de dossiers qui ressemble plus à la boîte de dialog Windows 7, avec la possibilité de copier et coller à partir d’une zone de texte en bas et du volet de navigation à gauche avec des favoris et des emplacements communs, vous pouvez y accéder. de manière très légère.

L’interface utilisateur de FolderBrowserDialog est très minimale:

entrer la description de l'image ici

Mais vous pouvez avoir ceci à la place:

entrer la description de l'image ici

Voici une classe qui ouvre un sélecteur de dossiers de type Vista à l’aide de l’interface IFileDialog privée .Net, sans utiliser directement l’interopérabilité dans le code (.Net s’en charge pour vous). Il revient à la boîte de dialog pré-Vista si ce n’est dans une version suffisamment élevée de Windows. Devrait fonctionner sous Windows 7, 8, 9, 10 et supérieur (théoriquement).

 using System; using System.Reflection; using System.Windows.Forms; namespace MyCoolCompany.Shuriken { ///  /// Present the Windows Vista-style open file dialog to select a folder. Fall back for older Windows Versions ///  public class FolderSelectDialog { private ssortingng _initialDirectory; private ssortingng _title; private ssortingng _fileName = ""; public ssortingng InitialDirectory { get { return ssortingng.IsNullOrEmpty(_initialDirectory) ? Environment.CurrentDirectory : _initialDirectory; } set { _initialDirectory = value; } } public ssortingng Title { get { return _title ?? "Select a folder"; } set { _title = value; } } public ssortingng FileName { get { return _fileName; } } public bool Show() { return Show(IntPtr.Zero); } /// Handle of the control or window to be the parent of the file dialog /// true if the user clicks OK public bool Show(IntPtr hWndOwner) { var result = Environment.OSVersion.Version.Major >= 6 ? VistaDialog.Show(hWndOwner, InitialDirectory, Title) : ShowXpDialog(hWndOwner, InitialDirectory, Title); _fileName = result.FileName; return result.Result; } private struct ShowDialogResult { public bool Result { get; set; } public ssortingng FileName { get; set; } } private static ShowDialogResult ShowXpDialog(IntPtr ownerHandle, ssortingng initialDirectory, ssortingng title) { var folderBrowserDialog = new FolderBrowserDialog { Description = title, SelectedPath = initialDirectory, ShowNewFolderButton = false }; var dialogResult = new ShowDialogResult(); if (folderBrowserDialog.ShowDialog(new WindowWrapper(ownerHandle)) == DialogResult.OK) { dialogResult.Result = true; dialogResult.FileName = folderBrowserDialog.SelectedPath; } return dialogResult; } private static class VistaDialog { private const ssortingng c_foldersFilter = "Folders|\n"; private const BindingFlags c_flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private readonly static Assembly s_windowsFormsAssembly = typeof(FileDialog).Assembly; private readonly static Type s_iFileDialogType = s_windowsFormsAssembly.GetType("System.Windows.Forms.FileDialogNative+IFileDialog"); private readonly static MethodInfo s_createVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("CreateVistaDialog", c_flags); private readonly static MethodInfo s_onBeforeVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("OnBeforeVistaDialog", c_flags); private readonly static MethodInfo s_getOptionsMethodInfo = typeof(FileDialog).GetMethod("GetOptions", c_flags); private readonly static MethodInfo s_setOptionsMethodInfo = s_iFileDialogType.GetMethod("SetOptions", c_flags); private readonly static uint s_fosPickFoldersBitFlag = (uint) s_windowsFormsAssembly .GetType("System.Windows.Forms.FileDialogNative+FOS") .GetField("FOS_PICKFOLDERS") .GetValue(null); private readonly static ConstructorInfo s_vistaDialogEventsConstructorInfo = s_windowsFormsAssembly .GetType("System.Windows.Forms.FileDialog+VistaDialogEvents") .GetConstructor(c_flags, null, new[] { typeof(FileDialog) }, null); private readonly static MethodInfo s_adviseMethodInfo = s_iFileDialogType.GetMethod("Advise"); private readonly static MethodInfo s_unAdviseMethodInfo = s_iFileDialogType.GetMethod("Unadvise"); private readonly static MethodInfo s_showMethodInfo = s_iFileDialogType.GetMethod("Show"); public static ShowDialogResult Show(IntPtr ownerHandle, ssortingng initialDirectory, ssortingng title) { var openFileDialog = new OpenFileDialog { AddExtension = false, CheckFileExists = false, DereferenceLinks = true, Filter = c_foldersFilter, InitialDirectory = initialDirectory, Multiselect = false, Title = title }; var iFileDialog = s_createVistaDialogMethodInfo.Invoke(openFileDialog, new object[] { }); s_onBeforeVistaDialogMethodInfo.Invoke(openFileDialog, new[] { iFileDialog }); s_setOptionsMethodInfo.Invoke(iFileDialog, new object[] { (uint) s_getOptionsMethodInfo.Invoke(openFileDialog, new object[] { }) | s_fosPickFoldersBitFlag }); var adviseParametersWithOutputConnectionToken = new[] { s_vistaDialogEventsConstructorInfo.Invoke(new object[] { openFileDialog }), 0U }; s_adviseMethodInfo.Invoke(iFileDialog, adviseParametersWithOutputConnectionToken); try { int retVal = (int) s_showMethodInfo.Invoke(iFileDialog, new object[] { ownerHandle }); return new ShowDialogResult { Result = retVal == 0, FileName = openFileDialog.FileName }; } finally { s_unAdviseMethodInfo.Invoke(iFileDialog, new[] { adviseParametersWithOutputConnectionToken[1] }); } } } // Wrap an IWin32Window around an IntPtr private class WindowWrapper : IWin32Window { private readonly IntPtr _handle; public WindowWrapper(IntPtr handle) { _handle = handle; } public IntPtr Handle { get { return _handle; } } } } } 

Je l’ai développé comme une version nettoyée de la boîte de dialog de sélection de dossiers de style .NET Win 7 par Bill Seddon de lyquidity.com (je n’ai aucune affiliation). J’ai écrit le mien car sa solution nécessite une classe Reflection supplémentaire qui n’est pas nécessaire dans ce but précis, utilise un contrôle de stream basé sur des exceptions, ne met pas en cache les résultats de ses appels de reflection. Notez que la classe statique VistaDialog nestede est telle que ses variables de reflection statiques n’essayent pas d’être remplies si la méthode Show n’est jamais appelée.

Il est utilisé comme tel dans un Windows Form:

 var dialog = new FolderSelectDialog { InitialDirectory = musicFolderTextBox.Text, Title = "Select a folder to import music from" }; if (dialog.Show(Handle)) { musicFolderTextBox.Text = dialog.FileName; } 

Vous pouvez bien sûr jouer avec ses options et ses propriétés. Par exemple, il permet une sélection multiple dans la boîte de dialog de style Vista.

Aussi, veuillez noter que Simon Mourier a donné une réponse qui montre comment faire exactement le même travail en utilisant l’interopérabilité contre l’API Windows directement, bien que sa version devrait être complétée pour utiliser l’ancienne boîte de dialog si dans une version antérieure de Windows. Malheureusement, je n’avais pas encore trouvé son poste lorsque j’ai mis au point ma solution. Nomme ton poison!

Vous pouvez simplement utiliser la classe FolderBrowserDialog partir de l’espace de noms System.Windows.Forms .

S’il vous plaît ne pas essayer de rouler votre propre avec une classe TreeView / DirectoryInfo. D’une part, il existe de nombreuses fonctionnalités intéressantes que vous obtenez gratuitement (icons / clic droit / réseaux) en utilisant SHBrowseForFolder. Pour un autre, il existe des cas / captures dont vous ne saurez probablement pas l’existence.

Vous pouvez utiliser un TreeView en combinaison avec la classe DirectoryInfo.

Pour plus de fonctionnalités que le FolderBrowserdialog, comme le filtrage, les cases à cocher, etc., jetez un coup d’œil aux contrôles tiers tels que Shell MegaPack . Comme ils sont des contrôles, ils peuvent être placés dans vos propres formulaires au lieu d’apparaître comme un dialog modal.

ou mieux encore, vous pouvez mettre ce code dans un fichier de classe

 using System; using System.IO; using System.Runtime.ComstackrServices; using System.Runtime.InteropServices; using System.Windows.Forms; internal class OpenFolderDialog : IDisposable { ///  /// Gets/sets folder in which dialog will be open. ///  public ssortingng InitialFolder { get; set; } ///  /// Gets/sets directory in which dialog will be open if there is no recent directory available. ///  public ssortingng DefaultFolder { get; set; } ///  /// Gets selected folder. ///  public ssortingng Folder { get; private set; } internal DialogResult ShowDialog(IWin32Window owner) { if (Environment.OSVersion.Version.Major >= 6) { return ShowVistaDialog(owner); } else { return ShowLegacyDialog(owner); } } private DialogResult ShowVistaDialog(IWin32Window owner) { var frm = (NativeMethods.IFileDialog)(new NativeMethods.FileOpenDialogRCW()); uint options; frm.GetOptions(out options); options |= NativeMethods.FOS_PICKFOLDERS | NativeMethods.FOS_FORCEFILESYSTEM | NativeMethods.FOS_NOVALIDATE | NativeMethods.FOS_NOTESTFILECREATE | NativeMethods.FOS_DONTADDTORECENT; frm.SetOptions(options); if (this.InitialFolder != null) { NativeMethods.IShellItem directoryShellItem; var riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); //IShellItem if (NativeMethods.SHCreateItemFromParsingName(this.InitialFolder, IntPtr.Zero, ref riid, out directoryShellItem) == NativeMethods.S_OK) { frm.SetFolder(directoryShellItem); } } if (this.DefaultFolder != null) { NativeMethods.IShellItem directoryShellItem; var riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); //IShellItem if (NativeMethods.SHCreateItemFromParsingName(this.DefaultFolder, IntPtr.Zero, ref riid, out directoryShellItem) == NativeMethods.S_OK) { frm.SetDefaultFolder(directoryShellItem); } } if (frm.Show(owner.Handle) == NativeMethods.S_OK) { NativeMethods.IShellItem shellItem; if (frm.GetResult(out shellItem) == NativeMethods.S_OK) { IntPtr pszSsortingng; if (shellItem.GetDisplayName(NativeMethods.SIGDN_FILESYSPATH, out pszSsortingng) == NativeMethods.S_OK) { if (pszSsortingng != IntPtr.Zero) { try { this.Folder = Marshal.PtrToSsortingngAuto(pszSsortingng); return DialogResult.OK; } finally { Marshal.FreeCoTaskMem(pszSsortingng); } } } } } return DialogResult.Cancel; } private DialogResult ShowLegacyDialog(IWin32Window owner) { using (var frm = new SaveFileDialog()) { frm.CheckFileExists = false; frm.CheckPathExists = true; frm.CreatePrompt = false; frm.Filter = "|" + Guid.Empty.ToSsortingng(); frm.FileName = "any"; if (this.InitialFolder != null) { frm.InitialDirectory = this.InitialFolder; } frm.OverwritePrompt = false; frm.Title = "Select Folder"; frm.ValidateNames = false; if (frm.ShowDialog(owner) == DialogResult.OK) { this.Folder = Path.GetDirectoryName(frm.FileName); return DialogResult.OK; } else { return DialogResult.Cancel; } } } public void Dispose() { } //just to have possibility of Using statement. } internal static class NativeMethods { #region Constants public const uint FOS_PICKFOLDERS = 0x00000020; public const uint FOS_FORCEFILESYSTEM = 0x00000040; public const uint FOS_NOVALIDATE = 0x00000100; public const uint FOS_NOTESTFILECREATE = 0x00010000; public const uint FOS_DONTADDTORECENT = 0x02000000; public const uint S_OK = 0x0000; public const uint SIGDN_FILESYSPATH = 0x80058000; #endregion #region COM [ComImport, ClassInterface(ClassInterfaceType.None), TypeLibType(TypeLibTypeFlags.FCanCreate), Guid("DC1C5A9C-E88A-4DDE-A5A1-60F82A20AEF7")] internal class FileOpenDialogRCW { } [ComImport(), Guid("42F85136-DB7E-439C-85F1-E4075D135FC8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IFileDialog { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] [PreserveSig()] uint Show([In, Optional] IntPtr hwndOwner); //IModalWindow [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFileTypes([In] uint cFileTypes, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr rgFilterSpec); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFileTypeIndex([In] uint iFileType); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetFileTypeIndex(out uint piFileType); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint Advise([In, MarshalAs(UnmanagedType.Interface)] IntPtr pfde, out uint pdwCookie); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint Unadvise([In] uint dwCookie); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetOptions([In] uint fos); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetOptions(out uint fos); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] void SetDefaultFolder([In, MarshalAs(UnmanagedType.Interface)] IShellItem psi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFolder([In, MarshalAs(UnmanagedType.Interface)] IShellItem psi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetFolder([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetCurrentSelection([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFileName([In, MarshalAs(UnmanagedType.LPWStr)] ssortingng pszName); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetFileName([MarshalAs(UnmanagedType.LPWStr)] out ssortingng pszName); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetTitle([In, MarshalAs(UnmanagedType.LPWStr)] ssortingng pszTitle); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetOkButtonLabel([In, MarshalAs(UnmanagedType.LPWStr)] ssortingng pszText); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFileNameLabel([In, MarshalAs(UnmanagedType.LPWStr)] ssortingng pszLabel); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetResult([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint AddPlace([In, MarshalAs(UnmanagedType.Interface)] IShellItem psi, uint fdap); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetDefaultExtension([In, MarshalAs(UnmanagedType.LPWStr)] ssortingng pszDefaultExtension); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint Close([MarshalAs(UnmanagedType.Error)] uint hr); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetClientGuid([In] ref Guid guid); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint ClearClientData(); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint SetFilter([MarshalAs(UnmanagedType.Interface)] IntPtr pFilter); } [ComImport, Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] internal interface IShellItem { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint BindToHandler([In] IntPtr pbc, [In] ref Guid rbhid, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out IntPtr ppvOut); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetParent([MarshalAs(UnmanagedType.Interface)] out IShellItem ppsi); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetDisplayName([In] uint sigdnName, out IntPtr ppszName); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint GetAtsortingbutes([In] uint sfgaoMask, out uint psfgaoAtsortingbs); [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] uint Compare([In, MarshalAs(UnmanagedType.Interface)] IShellItem psi, [In] uint hint, out int piOrder); } #endregion [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)] internal static extern int SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] ssortingng pszPath, IntPtr pbc, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out IShellItem ppv); } 

Et l’utiliser comme ça

 using (var frm = new OpenFolderDialog()) { if (frm.ShowDialog(this)== DialogResult.OK) { MessageBox.Show(this, frm.Folder); } } 

une meilleure façon de le faire et avec la sélection de plusieurs dossiers

 using System; using System.Reflection; using System.Windows.Forms; class OpenFolderDialog { //public bool AddExtension { get; set; } public bool AutoUpgradeEnabled { get; set; } //public bool CheckFileExists { get; set; } public bool CheckPathExists { get; set; } //public bool DereferenceLinks { get; set; } public ssortingng Title { get; set; } //public ssortingng DefaultExt { get; set; } public ssortingng InitialDirectory { get; set; } //public bool ValidateNames { get; set; } //public bool SupportMultiDottedExtensions { get; set; } //public bool ShowHelp { get; set; } public bool Multiselect { get; set; } public bool RestoreDirectory { get; set; } //public ssortingng Filter { get; set; } public ssortingng SelectedPath { get; private set; } public ssortingng[] SelectedPaths { get; private set; } private FolderBrowserDialog FolderBrowser { get { return new FolderBrowserDialog() { ShowNewFolderButton = true, Description = Title, SelectedPath = InitialDirectory }; } } private OpenFileDialog Dialog { get { return new OpenFileDialog() { Title = Title, AddExtension = false, AutoUpgradeEnabled = AutoUpgradeEnabled, CheckFileExists = true, CheckPathExists = CheckPathExists, DefaultExt = ssortingng.Empty, DereferenceLinks = false, InitialDirectory = InitialDirectory, ValidateNames = false, SupportMultiDottedExtensions = false, ShowHelp = false, Multiselect = Multiselect, RestoreDirectory = RestoreDirectory, Filter = ssortingng.Empty, FileName = SelectedPath }; } } /// Handle of the control or window to be the parent of the file dialog /// true if the user clicks OK public DialogResult ShowDialog(IntPtr hWndOwner) { if (Environment.OSVersion.Version.Major >= 6) { OpenFileDialog dialog = Dialog; DialogResult result = VistaDialog.Show(hWndOwner, dialog) != 0 ? DialogResult.Cancel : DialogResult.OK; SelectedPath = dialog.FileName; SelectedPaths = dialog.FileNames; return result; } else { FolderBrowserDialog XPDialog = FolderBrowser; DialogResult result = XPDialog.ShowDialog(); SelectedPath = XPDialog.SelectedPath; return result; } } private static class VistaDialog { private const BindingFlags c_flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private readonly static Assembly s_windowsFormsAssembly = typeof(FileDialog).Assembly; private readonly static Type s_iFileDialogType = s_windowsFormsAssembly.GetType("System.Windows.Forms.FileDialogNative+IFileDialog"); private readonly static MethodInfo s_createVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("CreateVistaDialog", c_flags); private readonly static MethodInfo s_onBeforeVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("OnBeforeVistaDialog", c_flags); private readonly static MethodInfo s_getOptionsMethodInfo = typeof(FileDialog).GetMethod("GetOptions", c_flags); private readonly static MethodInfo s_setOptionsMethodInfo = s_iFileDialogType.GetMethod("SetOptions", c_flags); private readonly static uint s_fosPickFoldersBitFlag = (uint)s_windowsFormsAssembly .GetType("System.Windows.Forms.FileDialogNative+FOS") .GetField("FOS_PICKFOLDERS") .GetValue(null); private readonly static ConstructorInfo s_vistaDialogEventsConstructorInfo = s_windowsFormsAssembly .GetType("System.Windows.Forms.FileDialog+VistaDialogEvents") .GetConstructor(c_flags, null, new[] { typeof(FileDialog) }, null); private readonly static MethodInfo s_adviseMethodInfo = s_iFileDialogType.GetMethod("Advise"); private readonly static MethodInfo s_unAdviseMethodInfo = s_iFileDialogType.GetMethod("Unadvise"); private readonly static MethodInfo s_showMethodInfo = s_iFileDialogType.GetMethod("Show"); public static int Show(IntPtr ownerHandle, OpenFileDialog dialog) { var iFileDialog = s_createVistaDialogMethodInfo.Invoke(dialog, new object[] { }); s_onBeforeVistaDialogMethodInfo.Invoke(dialog, new[] { iFileDialog }); s_setOptionsMethodInfo.Invoke(iFileDialog, new object[] { (uint)s_getOptionsMethodInfo.Invoke(dialog, new object[] { }) | s_fosPickFoldersBitFlag }); var adviseParametersWithOutputConnectionToken = new[] { s_vistaDialogEventsConstructorInfo.Invoke(new object[] { dialog }), 0U }; s_adviseMethodInfo.Invoke(iFileDialog, adviseParametersWithOutputConnectionToken); try { return (int)s_showMethodInfo.Invoke(iFileDialog, new object[] { ownerHandle }); } finally { s_unAdviseMethodInfo.Invoke(iFileDialog, new[] { adviseParametersWithOutputConnectionToken[1] }); } } } } 

usage:

 OpenFolderDialog folder = new OpenFolderDialog() { Title = "Select destination folder", AutoUpgradeEnabled = true, CheckPathExists = true, InitialDirectory = Environment.GetFolderPath (Environment.SpecialFolder.DesktopDirectory), Multiselect = true, RestoreDirectory = true }; DialogResult result = folder.ShowDialog(IntPtr.Zero); if (result.Equals(DialogResult.OK)) MessageBox.Show(folder.SelectedPath + " Paths:" + folder.SelectedPaths.Length);