根据内容过滤 OpenFileDialog 中显示的文件?
有没有办法修改 OpenFileDialog 的行为,以便它查看它打开的文件夹中的文件,然后根据其内容忽略某些文件?
一个示例是打开一个充满 Zip 文件的文件夹,但仅显示包含特定文件的文件夹。
从文档中,有 HookProc 但我不太确定如何使用它。
请注意,如果可能的话,我意识到这将是一个相对较慢的操作。 目前我并不关心性能。
谢谢!
Is there a way to modify the behavior of the OpenFileDialog so that it looks inside the files in the folder it opens to and then ignores certain ones based on their content?
One example would be to open to a folder full of Zip files but only show the ones that contain a certain file.
From the documentation, there's the HookProc but I'm not exactly sure how I'd use it.
Please note that if it is possible, I realize that it'll be a relatively slow operation. At the moment I'm not concerned about performance.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会忽视 OpenFileDialog 的复杂性。 建立一个真正有效的系统并不容易。 当您构建自己的对话框时,它不是“正常”对话框,因此会让用户感到困惑。 即使你做得很好也是如此,这是困难的。 因此,我建议您坚持扩展已有的内容,而不是编写新的内容。
检查这篇文章了解 OFD 的扩展,可能/可以进行调整正是您想要的。 您用 C# 编写了一个回调来响应路径选择。
相关:FolderBrowserDialogEx是FolderBrowserDialog上的类似扩展。 不管名称如何,您都可以将其配置为搜索文件和文件夹。 当选择某些内容(文件夹、文件)时会调用一个回调,在该回调中您可以执行您需要执行的操作。 例如,查看文件夹内的文件并填充文件列表以仅显示这些文件。
您可以考虑的另一个选择是Ookii 的对话框库。 这是 OpenFileDialog 的开源实现,它包含 Vista 中所有新对话框内容的 COM 包装器。 使用该库,您可以弹出 Vista OpenFileDialog 并从 IFileDialogEvents 接口,采用 C# 代码。 OnFolderChange() 就是这样的事件之一。 在处理程序中,您可以调用 IFolder.GetFolder() ,这将为您提供 IShellItem,它为您提供用户要更改到的文件夹。 下一步是逐项列出并可能过滤文件集,这是我将留给读者的练习......
I wouldn't dismiss the complexity of the OpenFileDialog. It's not so easy to build one that really works. When you do build your own, it's not the "normal" dialog and as a result it confuses users. This is true even if you do it well, which is difficult. So I'd suggest you stick to extending what is already there, rather than writing something new.
Check this article for an extension of OFD that might/could be tweaked to do exactly what you want. There's a callback that you write in C# that responds to path selection.
Related: FolderBrowserDialogEx is a similar extension on FolderBrowserDialog. Despite the name, you can configure it to search for files, as well as folders. There's a callback that gets invoked when something (a folder, a file) is selected, and within that callback you can do what you need to do. For example, peek inside the files within a folder and populate the list of files to display with only those files.
Another option you might consider is the dialog library from Ookii. This is an open source implementation of the OpenFileDialog, and it includes COM wrappers for all the new dialog stuff in Vista. Using that library you can pop a Vista OpenFileDialog and receive events from the IFileDialogEvents interface, in C# code. One such event is OnFolderChange(). Within the handler you could call IFolder.GetFolder() which will get you an IShellItem, which gives you the folder the user is changing to. The next step would be to itemize and potentially filter the set of files, which is an exercise I will leave to the reader...
不,您必须为此实现自己的功能。 但说实话,OpenFileDialog 确实没有做很多事情。 老实说,是的,您可能可以使用它,但是当真正的工作是检查文件的内容时,您会徒劳地做很多工作,然后您可以在上面编写自己的简单 OpenFileDialog 类那。
您可能会发现这个问题对于列出 zip 文件的内容很有帮助:
如何在 c# 中列出 .zip 文件夹的内容?
(注意,您可能会对其进行线程化以提高性能,只是不要跨越多个线程)
No, you would have to implement your own functionality for that. But to be honest, the OpenFileDialog really doesn't do a whole lot anyway. To be honest, yeah, you probably could hook into it, but you'd be doing a lot of work for nothing when the real work is to inspect the content of the files and then you can write your own simple OpenFileDialog class on top of that.
You might find this question helpful regarding listing contents of zip files:
How to list the contents of a .zip folder in c#?
(Note, you could potentially thread it to improve performance, just don't span many threads)
您也许可以使用 Windows API 代码包(随源代码一起提供)。 与 Winforms/WPF 中的版本相比,通用文件对话框功能公开了更多的文件对话框功能。
http://code.msdn.microsoft.com/WindowsAPICodePack
You can probably use the Windows API Code Pack (comes with the source). The Common File dialogs feature exposes a lot more functionality of file dialogs than the versions in Winforms/WPF.
http://code.msdn.microsoft.com/WindowsAPICodePack