C#自带的文件资源浏览器要么只能选择文件,要么只能选择文件夹无法手动输入路径,想实现一个像windows自带的文件资源管理器类似文件夹浏览器。

/// <summary>/// Wraps System.Windows.Forms.OpenFileDialog to make it present/// a vista-style dialog./// </summary>public class FolderSelectDialog{// Wrapped dialogSystem.Windows.Forms.OpenFileDialog ofd = null;/// <summary>/// Default constructor/// </summary>public FolderSelectDialog(){ofd = new System.Windows.Forms.OpenFileDialog();ofd.Filter = "Folders|\n";ofd.AddExtension = false;ofd.CheckFileExists = false;ofd.DereferenceLinks = true;ofd.Multiselect = false;}#region Properties/// <summary>/// Gets/Sets the initial folder to be selected. A null value selects the current directory./// </summary>public string InitialDirectory{get { return ofd.InitialDirectory; }set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }}/// <summary>/// Gets/Sets the title to show in the dialog/// </summary>public string Title{get { return ofd.Title; }set { ofd.Title = value == null ? "Select a folder" : value; }}/// <summary>/// Gets the selected folder/// </summary>public string FileName{get { return ofd.FileName; }}#endregion#region Methods/// <summary>/// Shows the dialog/// </summary>/// <returns>True if the user presses OK else false</returns>public bool ShowDialog(){return ShowDialog(IntPtr.Zero);}/// <summary>/// Shows the dialog/// </summary>/// <param name="hWndOwner">Handle of the control to be parent</param>/// <returns>True if the user presses OK else false</returns>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;}#endregion}/// <summary>/// Creates IWin32Window around an IntPtr/// </summary>public class WindowWrapper : System.Windows.Forms.IWin32Window{/// <summary>/// Constructor/// </summary>/// <param name="handle">Handle to wrap</param>public WindowWrapper(IntPtr handle){_hwnd = handle;}/// <summary>/// Original ptr/// </summary>public IntPtr Handle{get { return _hwnd; }}private IntPtr _hwnd;}/// <summary>/// This class is from the Front-End for Dosbox and is used to present a 'vista' dialog box to select folders./// Being able to use a vista style dialog box to select folders is much better then using the shell folder browser./// http://code.google.com/p/fed/////// Example:/// var r = new Reflector("System.Windows.Forms");/// </summary>public class Reflector{#region variablesstring m_ns;Assembly m_asmb;#endregion#region Constructors/// <summary>/// Constructor/// </summary>/// <param name="ns">The namespace containing types to be used</param>public Reflector(string ns): this(ns, ns){ }/// <summary>/// Constructor/// </summary>/// <param name="an">A specific assembly name (used if the assembly name does not tie exactly with the namespace)</param>/// <param name="ns">The namespace containing types to be used</param>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;}}}#endregion#region Methods/// <summary>/// Return a Type instance for a type 'typeName'/// </summary>/// <param name="typeName">The name of the type</param>/// <returns>A type instance</returns>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;}/// <summary>/// Create a new object of a named type passing along any params/// </summary>/// <param name="name">The name of the type to create</param>/// <param name="parameters"></param>/// <returns>An instantiated type</returns>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;}/// <summary>/// Calls method 'func' on object 'obj' passing parameters 'parameters'/// </summary>/// <param name="obj">The object on which to excute function 'func'</param>/// <param name="func">The function to execute</param>/// <param name="parameters">The parameters to pass to function 'func'</param>/// <returns>The result of the function invocation</returns>public object Call(object obj, string func, params object[] parameters){return Call2(obj, func, parameters);}/// <summary>/// Calls method 'func' on object 'obj' passing parameters 'parameters'/// </summary>/// <param name="obj">The object on which to excute function 'func'</param>/// <param name="func">The function to execute</param>/// <param name="parameters">The parameters to pass to function 'func'</param>/// <returns>The result of the function invocation</returns>public object Call2(object obj, string func, object[] parameters){return CallAs2(obj.GetType(), obj, func, parameters);}/// <summary>/// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'/// </summary>/// <param name="type">The type of 'obj'</param>/// <param name="obj">The object on which to excute function 'func'</param>/// <param name="func">The function to execute</param>/// <param name="parameters">The parameters to pass to function 'func'</param>/// <returns>The result of the function invocation</returns>public object CallAs(Type type, object obj, string func, params object[] parameters){return CallAs2(type, obj, func, parameters);}/// <summary>/// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'/// </summary>/// <param name="type">The type of 'obj'</param>/// <param name="obj">The object on which to excute function 'func'</param>/// <param name="func">The function to execute</param>/// <param name="parameters">The parameters to pass to function 'func'</param>/// <returns>The result of the function invocation</returns>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);}/// <summary>/// Returns the value of property 'prop' of object 'obj'/// </summary>/// <param name="obj">The object containing 'prop'</param>/// <param name="prop">The property name</param>/// <returns>The property value</returns>public object Get(object obj, string prop){return GetAs(obj.GetType(), obj, prop);}/// <summary>/// Returns the value of property 'prop' of object 'obj' which has type 'type'/// </summary>/// <param name="type">The type of 'obj'</param>/// <param name="obj">The object containing 'prop'</param>/// <param name="prop">The property name</param>/// <returns>The property value</returns>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);}/// <summary>/// Returns an enum value/// </summary>/// <param name="typeName">The name of enum type</param>/// <param name="name">The name of the value</param>/// <returns>The enum value</returns>public object GetEnum(string typeName, string name){Type type = GetType(typeName);FieldInfo fieldInfo = type.GetField(name);return fieldInfo.GetValue(null);}#endregion}
复制到项目中即可使用

使用方法

FolderSelectDialog dialog = new FolderSelectDialog();
dialog.ShowDialog();
this.txt1.Text = this.txt1.Text + " " + dialog.FileName;
p.文件路径 = this.txt1.Text;


就可以实现这样的选择文件夹效果了。
转载自https://www.cnblogs.com/xyz0835/p/5056464.html

强烈推荐C#打开文件夹openfolder自定义类(不是browser也不是openfile哦~)自带的实在不好用相关推荐

  1. 如何批量重命名文件夹,自定义修改文件夹的名称

    有些小伙伴还不知道如何批量修改文件夹名称,就只能采用一个一个修改的方法,这种发光法的速度是最慢的,需要消耗大量的时间,小编非常的不推荐,今天教大家一个新的改名技巧,自定义批量修改文件夹的名称. 第一步 ...

  2. linux终端如何打开文件夹,如何从终端打开文件夹(带GUI)?

    问题描述 我想在我的统一面板(ubuntu 12.10)中放置一个链接/快捷方式/启动器. 我在handytutorial.com上按照this tutorial创建了一个自定义启动器并将其拖到面板上 ...

  3. VB 打开文件夹,并选中指定的文件

    标记一下,以防忘记. 这个功能比较方便,在打开文件夹时自动选中目标项,迅雷下载文件完成后的"打开文件夹"功能就是这样. 实现方面很简单,就是在调用EXPLORER时加个/Selec ...

  4. 计算机管理器用户怎么打开文件,电脑文件管理器怎么打开文件夹 文件管理器打开想要的文件夹方法-电脑教程...

    电脑文件管理器怎么打开文件夹?当我们在Win10中打开任务栏中文件管理器时,默认只有2个位置:此电脑和快速访问.前者指向传统的磁盘盘符界面,而后者就是Win10新增的那6个大家不怎么使用的固定默认文件 ...

  5. C++ 打开文件夹对话框-OPENFILENAME

    效果图: 实现方式 #include<iostream> #include<cstring> #include<string> #include<string ...

  6. win7 计算机打开无响应,怎么解决Win7打开文件夹无响应

    很多用户在操作的过程中经常遇到无响应假死的状态,那么怎么解决Win7打开文件夹无响应呢?就让学习啦小编来告诉大家解决Win7打开文件夹无响应的方法吧,希望对大家有所帮助. 解决Win7打开文件夹无响应 ...

  7. win10访问服务器文件夹慢,Win10专业版系统打开文件夹很慢的解决方法 - Win10专业版官网...

    现在有许多人开始使用win10专业版系统,但是有部分人电脑升级至win10专业版系统后,打开文件的速度十分缓慢,甚至会出现停滞假死的情况,十分影响电脑操作,不知道该如何解决.我们依照1.win+R 打 ...

  8. win10打开文件夹速度慢怎么办

    在使用电脑一段时间后,有时候就会出现打开文件夹速度很慢的情况,大部分时间是由于文件夹中的文件太多造成的,所以在日常生活中我们要学会对不同的文件分别归类,下面就教大家如何处理打开文件速度慢的问题 工具/ ...

  9. 将Vscode添加右键打开文件夹功能

    将Vscode添加右键打开文件夹功能 文章目录 将Vscode添加右键打开文件夹功能 前言 1. 将Vscode添加右键打开文件夹功能 1.1 第一种方法 1.2 第二种方法 总结 前言 想要鼠标右击 ...

最新文章

  1. 正则匹配以除了开头和结尾要有个大写_27.Google analytics 中的 正则表达式
  2. 安防行业为何缺少真正适用的AI芯片?
  3. 两对光纤收发器用网线连接_光纤那么快,路由器和电脑之间为何不用光纤连接,反而用普通网线...
  4. 压缩感知(Compressive Sensing)学习之(一)
  5. SharePoint 2007/2010 的SPGridView 控件常见的两个问题
  6. 如何让Element UI的Message消息提示每次只弹出一个
  7. Mac 获取 Brew
  8. Delphi多媒体设计之TMediaPlayer组件(二)
  9. 两轮小车相关记录(重点)
  10. Native方式运行Fabric(非Docker方式)
  11. 翻译:通过使用终端(iTerm2&Oh my ZSH)来提高您的生产率
  12. 智慧校园家校综合信息化管理系统平台
  13. python实验报告_实验一Python程序实验报告
  14. 计算机网络原理 读书笔记
  15. Segmentation-Based Deep-Learning Approach for Surface-Defect Detection-论文阅读笔记
  16. 【Hinton大神新作】Dynamic Routing Between Capsules阅读笔记
  17. T大计算机科学本科参考书目
  18. Subsonic介绍及使用
  19. android手机系统怎么刷机包,安卓手机系统怎么重装刷机
  20. tilemap 导入unity_unity的Tilemap学习笔记

热门文章

  1. 用python写《外星人入侵》游戏:武装飞船 >1
  2. 2022赛规整理——飞镖
  3. 跟着 Cell 学作图 | 3.箱线图+散点+差异显著性检验
  4. Android room 学习
  5. Oracle之旅-用户管理
  6. 章节十一:定时与邮件
  7. Micron 24 Gbps GDDR6X memory for GeForce RTX 40 series is now in production 【搬运外媒VedioCardz报道(手工翻译)】
  8. (转)如何做文献综述:克雷斯威尔五步文献综述法
  9. python下对hsv颜色空间进行量化
  10. C语言 函数指针和指针函数用法