在 C# 中浏览目录
如何向用户呈现一个控件,允许他/她选择目录?
似乎没有任何本机 .net 控件可以执行此操作?
How can I present a control to the user that allows him/her to select a directory?
There doesn't seem to be any native .net controls which do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只是一个更新,如果您使用 .Net 8(或更高版本),那么您现在可以使用 OpenFolderDialog(),它使用较新的 UI:
Just an update, if you are using .Net 8 (or higher) then you can now use the OpenFolderDialog() which uses the newer UI:
或者更好的是,您可以将此代码放入类文件中
并像这样使用它
or even more better, you can put this code in a class file
And use it like this
请不要尝试使用 TreeView/DirectoryInfo 类来创建自己的类。 一方面,通过使用 SHBrowseForFolder,您可以免费获得许多不错的功能(图标/右键单击/网络)。 另一方面,有一些你可能没有意识到的边缘情况/问题。
Please don't try and roll your own with a TreeView/DirectoryInfo class. For one thing there are many nice features you get for free (icons/right-click/networks) by using SHBrowseForFolder. For another there are a edge cases/catches you will likely not be aware of.
您可以只使用
System.Windows.Forms
命名空间中的FolderBrowserDialog
类。You could just use the
FolderBrowserDialog
class from theSystem.Windows.Forms
namespace.FolderBrowserDialog 类 是最佳选择。
The FolderBrowserDialog class is the best option.
注意:不能保证此代码可以在 .Net 框架的未来版本中运行。 总体而言,通过反射使用私有 .Net 框架内部结构可能并不好。 使用底部提到的互操作解决方案,因为 Windows API 不太可能更改。
如果您正在寻找看起来更像 Windows 7 对话框的文件夹选择器,请使用能够从底部的文本框和左侧的导航窗格复制和粘贴收藏夹和常用位置,然后您可以以非常轻量级的方式访问它。
FolderBrowserDialog UI 非常小:
但您可以使用此替代:
这是一个使用 .Net 专用
IFileDialog 接口,无需在代码中直接使用互操作(.Net 会为您处理好)。 如果 Windows 版本不够高,它会退回到 Vista 之前的对话框。 应该适用于 Windows 7、8、9、10 及更高版本(理论上)。
我将其开发为 .NET Win 7 样式文件夹选择对话框 的清理版本作者:lyquidity.com 的 Bill Seddon(我没有隶属关系)。 我编写了自己的解决方案,因为他的解决方案需要一个额外的 Reflection 类,而该类不需要用于此重点目的,使用基于异常的流控制,不缓存其反射调用的结果。 请注意,嵌套静态
VistaDialog
类的目的是,如果从未调用Show
方法,则其静态反射变量不会尝试填充。它在 Windows 窗体中的使用方式如下:
您当然可以尝试使用它的选项以及它公开的属性。 例如,它允许在 Vista 风格的对话框中进行多项选择。
另请注意,Simon Mourier 给出了答案,该答案展示了如何直接使用针对 Windows API 的互操作来完成完全相同的工作,但如果在旧版本的 Windows 中,则必须对其版本进行补充才能使用旧样式对话框。 不幸的是,当我制定解决方案时,我还没有找到他的帖子。 说出你的毒药!
Note: there is no guarantee this code will work in future versions of the .Net framework. Using private .Net framework internals as done here through reflection is probably not good overall. Use the interop solution mentioned at the bottom, as the Windows API is less likely to change.
If you are looking for a Folder picker that looks more like the Windows 7 dialog, with the ability to copy and paste from a textbox at the bottom and the navigation pane on the left with favorites and common locations, then you can get access to that in a very lightweight way.
The FolderBrowserDialog UI is very minimal:
But you can have this instead:
Here's a class that opens a Vista-style folder picker using the .Net private
IFileDialog
interface, without directly using interop in the code (.Net takes care of that for you). It falls back to the pre-Vista dialog if not in a high enough Windows version. Should work in Windows 7, 8, 9, 10 and higher (theoretically).I developed this as a cleaned up version of .NET Win 7-style folder select dialog by Bill Seddon of lyquidity.com (I have no affiliation). I wrote my own because his solution requires an additional Reflection class that isn't needed for this focused purpose, uses exception-based flow control, doesn't cache the results of its reflection calls. Note that the nested static
VistaDialog
class is so that its static reflection variables don't try to get populated if theShow
method is never called.It is used like so in a Windows Form:
You can of course play around with its options and what properties it exposes. For example, it allows multiselect in the Vista-style dialog.
Also, please note that Simon Mourier gave an answer that shows how to do the exact same job using interop against the Windows API directly, though his version would have to be supplemented to use the older style dialog if in an older version of Windows. Unfortunately, I hadn't found his post yet when I worked up my solution. Name your poison!