C# 文件夹选择弹框

1. 文件夹-目录列表选择弹框

1     //1.传统的选择目录弹框
2     FolderBrowserDialog dialog = new FolderBrowserDialog();
3     dialog.Description = "请选择文件路径";
4     dialog.RootFolder = Environment.SpecialFolder.Desktop;
5     if (dialog.ShowDialog() == DialogResult.OK)
6     {
7         var selectFolder = dialog.SelectedPath;
8 
9     }

2.WindowsAPI,widnow内置的弹框

nuget引用Windows7APICodePack包,设置属性IsFolderPicker,就能选择文件夹了

1     CommonOpenFileDialog openFolderDialog = new CommonOpenFileDialog
2     {
3         IsFolderPicker = true, InitialDirectory = UtilsCommonPath.GetDesktopFolder()
4     };
5     if (openFolderDialog.ShowDialog(Window.GetWindow(this)) == CommonFileDialogResult.Ok)
6     {
7         var selectFolder = openFolderDialog.FileName;
8     }

3. 自定义文件夹选择框(修改System.Windows.Forms.OpenFileDialog)

  1 using System;
  2 using System.Reflection;
  3 using System.Windows.Forms;
  4 
  5 namespace Utils {
  6     /// <summary>
  7     /// Present the Windows Vista-style open file dialog to select a folder. Fall back for older Windows Versions
  8     /// </summary>
  9     public class FolderSelectDialog {
 10         private string _initialDirectory;
 11         private string _title;
 12         private string _fileName = "";
 13 
 14         public string InitialDirectory {
 15             get { return string.IsNullOrEmpty(_initialDirectory) ? Environment.CurrentDirectory : _initialDirectory; }
 16             set { _initialDirectory = value; }
 17         }
 18         public string Title {
 19             get { return _title ?? "Select a folder"; }
 20             set { _title = value; }
 21         }
 22         public string FileName { get { return _fileName; } }
 23 
 24         public bool Show() { return Show(IntPtr.Zero); }
 25 
 26         /// <param name="hWndOwner">Handle of the control or window to be the parent of the file dialog</param>
 27         /// <returns>true if the user clicks OK</returns>
 28         public bool Show(IntPtr hWndOwner) {
 29             var result = Environment.OSVersion.Version.Major >= 6
 30                 ? VistaDialog.Show(hWndOwner, InitialDirectory, Title)
 31                 : ShowXpDialog(hWndOwner, InitialDirectory, Title);
 32             _fileName = result.FileName;
 33             return result.Result;
 34         }
 35 
 36         private struct ShowDialogResult {
 37             public bool Result { get; set; }
 38             public string FileName { get; set; }
 39         }
 40 
 41         private static ShowDialogResult ShowXpDialog(IntPtr ownerHandle, string initialDirectory, string title) {
 42             var folderBrowserDialog = new FolderBrowserDialog {
 43                 Description = title,
 44                 SelectedPath = initialDirectory,
 45                 ShowNewFolderButton = false
 46             };
 47             var dialogResult = new ShowDialogResult();
 48             if (folderBrowserDialog.ShowDialog(new WindowWrapper(ownerHandle)) == DialogResult.OK) {
 49                 dialogResult.Result = true;
 50                 dialogResult.FileName = folderBrowserDialog.SelectedPath;
 51             }
 52             return dialogResult;
 53         }
 54 
 55         private static class VistaDialog {
 56             private const string c_foldersFilter = "Folders|\n";
 57 
 58             private const BindingFlags c_flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
 59             private readonly static Assembly s_windowsFormsAssembly = typeof(FileDialog).Assembly;
 60             private readonly static Type s_iFileDialogType = s_windowsFormsAssembly.GetType("System.Windows.Forms.FileDialogNative+IFileDialog");
 61             private readonly static MethodInfo s_createVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("CreateVistaDialog", c_flags);
 62             private readonly static MethodInfo s_onBeforeVistaDialogMethodInfo = typeof(OpenFileDialog).GetMethod("OnBeforeVistaDialog", c_flags);
 63             private readonly static MethodInfo s_getOptionsMethodInfo = typeof(FileDialog).GetMethod("GetOptions", c_flags);
 64             private readonly static MethodInfo s_setOptionsMethodInfo = s_iFileDialogType.GetMethod("SetOptions", c_flags);
 65             private readonly static uint s_fosPickFoldersBitFlag = (uint) s_windowsFormsAssembly
 66                 .GetType("System.Windows.Forms.FileDialogNative+FOS")
 67                 .GetField("FOS_PICKFOLDERS")
 68                 .GetValue(null);
 69             private readonly static ConstructorInfo s_vistaDialogEventsConstructorInfo = s_windowsFormsAssembly
 70                 .GetType("System.Windows.Forms.FileDialog+VistaDialogEvents")
 71                 .GetConstructor(c_flags, null, new[] { typeof(FileDialog) }, null);
 72             private readonly static MethodInfo s_adviseMethodInfo = s_iFileDialogType.GetMethod("Advise");
 73             private readonly static MethodInfo s_unAdviseMethodInfo = s_iFileDialogType.GetMethod("Unadvise");
 74             private readonly static MethodInfo s_showMethodInfo = s_iFileDialogType.GetMethod("Show");
 75 
 76             public static ShowDialogResult Show(IntPtr ownerHandle, string initialDirectory, string title) {
 77                 var openFileDialog = new OpenFileDialog {
 78                     AddExtension = false,
 79                     CheckFileExists = false,
 80                     DereferenceLinks = true,
 81                     Filter = c_foldersFilter,
 82                     InitialDirectory = initialDirectory,
 83                     Multiselect = false,
 84                     Title = title
 85                 };
 86 
 87                 var iFileDialog = s_createVistaDialogMethodInfo.Invoke(openFileDialog, new object[] { });
 88                 s_onBeforeVistaDialogMethodInfo.Invoke(openFileDialog, new[] { iFileDialog });
 89                 s_setOptionsMethodInfo.Invoke(iFileDialog, new object[] { (uint) s_getOptionsMethodInfo.Invoke(openFileDialog, new object[] { }) | s_fosPickFoldersBitFlag });
 90                 var adviseParametersWithOutputConnectionToken = new[] { s_vistaDialogEventsConstructorInfo.Invoke(new object[] { openFileDialog }), 0U };
 91                 s_adviseMethodInfo.Invoke(iFileDialog, adviseParametersWithOutputConnectionToken);
 92 
 93                 try {
 94                     int retVal = (int) s_showMethodInfo.Invoke(iFileDialog, new object[] { ownerHandle });
 95                     return new ShowDialogResult {
 96                         Result = retVal == 0,
 97                         FileName = openFileDialog.FileName
 98                     };
 99                 }
100                 finally {
101                     s_unAdviseMethodInfo.Invoke(iFileDialog, new[] { adviseParametersWithOutputConnectionToken[1] });
102                 }
103             }
104         }
105 
106         // Wrap an IWin32Window around an IntPtr
107         private class WindowWrapper : IWin32Window {
108             private readonly IntPtr _handle;
109             public WindowWrapper(IntPtr handle) { _handle = handle; }
110             public IntPtr Handle { get { return _handle; } }
111         }
112     }
113 }
View Code
1     //3.自定义文件夹框
2     var dialog = new FolderSelectDialog
3     {
4         Title = "选择文件夹"
5     };
6     if (dialog.Show())
7     {
8         var selectFolder = dialog.FileName;
9     }

 

posted @ 2020-08-24 11:48  唐宋元明清2188  阅读(696)  评论(0编辑  收藏  举报