从 .NET 中的同一对话框中选择文件或文件夹
是否有一种“简单”的方法可以从同一对话框中选择文件或文件夹?
在我创建的许多应用程序中,我允许文件或文件夹作为输入。 到目前为止,我总是最终创建一个开关来在文件或文件夹选择对话框之间切换,或者仅使用拖放功能。
因为这似乎是一个基本的事情,我想它以前已经创建过,但谷歌搜索并没有产生太多信息。 所以看起来我需要从头开始并创建一个自定义选择对话框,但我不想通过为这样一个琐碎的任务重新发明轮子来引入任何问题。
有人有任何提示或现有解决方案吗?
为了保持 UI 的一致性,如果能够扩展 OpenFileDialog(或FolderBrowserDialog)就好了。
Is there an "easy" way to select either a file OR a folder from the same dialog?
In many apps I create I allow for both files or folders as input.
Until now i always end up creating a switch to toggle between file or folder selection dialogs or stick with drag-and-drop functionality only.
Since this seems such a basic thing i would imagine this has been created before, but googling does not result in much information. So it looks like i would need to start from scratch and create a custom selection Dialog, but I rather not introduce any problems by reinventing the wheel for such a trivial task.
Anybody any tips or existing solutions?
To keep the UI consistent it would be nice if it is possible to extend the OpenFileDialog (or the FolderBrowserDialog).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
从技术上来说,这是可能的。 FolderBrowseDialog 使用的 shell 对话框能够返回文件和文件夹。 不幸的是,.NET 中并未公开该功能。 甚至反射也无法戳到所需的选项标志。
为了使其工作,您必须在 BROWSEINFO.ulFlags 中打开 BIF_BROWSEINCLUDEFILES 标志(值 = 0x4000)的情况下 P/Invoke SHBrowseForFolder()。 P/Invoke 是坚韧不拔的,最好从 另一个来源 或在 Reflector 的帮助下使用FolderBrowseDialog 类本身。
Technically, it is possible. The shell dialog used by FolderBrowseDialog has the ability to return both files and folders. Unfortunately, that capability isn't exposed in .NET. Not even reflection can poke the required option flag.
To make it work, you'd have to P/Invoke SHBrowseForFolder() with the BIF_BROWSEINCLUDEFILES flag turned on in BROWSEINFO.ulFlags (value = 0x4000). The P/Invoke is gritty, it is best to copy and paste the code from another source or the FolderBrowseDialog class itself with Reflector's help.
根据上述提示,我在以下位置找到了一些使用标准文件夹浏览器对话框的工作代码: http://topic.csdn.net/t/20020703/05/845468.html
扩展文件夹浏览器对话框的类
扩展对话框的实现代码
Based on the above tips I found some working code that uses the standard Folder Browser dialog at the following location: http://topic.csdn.net/t/20020703/05/845468.html
The Class for the extended Folder Browser Dialog
The code to implement the extended dialog
您可以使用标准 OpenFileDialog 来选择文件夹。 这是 CodeProject 中的一篇文章,演示了一种实现此操作的方法 (http://www. codeproject.com/KB/dialog/OpenFileOrFolderDialog.aspx)。
You can use standard OpenFileDialog to select a folder. Here is an article in CodeProject that demonstrated a way to do it (http://www.codeproject.com/KB/dialog/OpenFileOrFolderDialog.aspx).
已经完成了。 您可以使用 FolderBrowserDialogEx -
内置FolderBrowserDialog 的可重复使用的衍生产品。 该选项允许您输入路径,甚至是 UNC 路径。 您可以浏览文件夹或文件+文件夹。 您可以用它浏览计算机或打印机。 基于内置 FBD,但是……更好。 更灵活。 如果单击 GUI 中的文件夹,路径将显示在文本框中。 如果您键入路径,该文件夹就会被激活。 内置对话框缺少很多选项。
完整源代码。 自由的。 MS-公共许可证。
使用它的代码:
It's been done. You can use FolderBrowserDialogEx -
a re-usable derivative of the built-in FolderBrowserDialog. This one allows you to type in a path, even a UNC path. You can browse for folders, or files+folders. You can browse for computers or printers with it. Based on the built-in FBD, but ... better. More flexible. If you click a folder in the GUI, the path appears in the textbox. If you key in a path, the folder gets activatied. Lots of options the built-in dialog lacks.
Full Source code. Free. MS-Public license.
Code to use it:
AFAIK,.NET 框架中没有任何东西可以开箱即用地执行此操作。
.NET 文件对话框源自 CommonDialog :
AFAIK, there is nothing in the .NET framework that does this out of the box.
The .NET file dialogs derive from CommonDialog:
所有内置对话框都使用与其操作相对应的 shell API,例如 PrintDialog、OpenFileDialog、SaveFileDialog 等...
您很可能必须手动构建此功能。
All of the built in dialogs use the shell API's that correspond to their action, PrintDialog, OpenFileDialog, SaveFileDialog, etc...
You would most likely have to manually build this functionality.
http://www.pinvoke.net/default.aspx/shell32.shbrowseforfolder
这是 gerat 链接,如果您对此示例进行更改
,
您将得到您想要的
http://www.pinvoke.net/default.aspx/shell32.shbrowseforfolder
here is gerat link if you change in this sample
for
you will get what you want
如果您只想显示特定文件类型,请使用以下 文章(带有 C# 源代码)可以帮助您:
http://www .codeproject.com/KB/shell/csdoesshell1.aspx?fid=14137&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26
它还解释了其他选项可用于“自定义”FolderBrowser 对话框,
If you would like to display only specific file types, the following article (with source code in C#) can help you:
http://www.codeproject.com/KB/shell/csdoesshell1.aspx?fid=14137&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26
It also explains the other options that are available for "customizing" the FolderBrowser dialog,
这将允许您使用 OpenFileDialog 选择文件夹
this will allow you to select folders using OpenFileDialog
Ookii 对话框 实现了一个文件夹浏览器对话框,允许文件或文件夹作为输入,并且可用于 Windows 窗体和 WPF。
The Ookii Dialogs implement a folder browser dialog that allow for both files or folders as input, and is available for Windows Forms and WPF.