简体   繁体   English

根据内容过滤OpenFileDialog中显示的文件?

[英]Filtering the files shown in OpenFileDialog based on content?

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? 有没有办法修改OpenFileDialog的行为,以便它查看它打开的文件夹中的文件,然后根据其内容忽略某些文件?

One example would be to open to a folder full of Zip files but only show the ones that contain a certain file. 一个例子是打开一个充满Zip文件的文件夹,但只显示包含某个文件的文件夹。

From the documentation, there's the HookProc but I'm not exactly sure how I'd use it. 从文档中,有HookProc,但我不确定我是如何使用它的。

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! 谢谢!

I wouldn't dismiss the complexity of the OpenFileDialog. 我不会忽视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. 查看这篇文章,了解OFD的扩展 ,可能/可以进行调整以完全按照您的需要进行操作。 There's a callback that you write in C# that responds to path selection. 你在C#中编写了一个响应路径选择的回调函数。

Related: FolderBrowserDialogEx is a similar extension on FolderBrowserDialog. 相关: FolderBrowserDialogExFolderBrowserDialog上的类似扩展。 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 . 您可能考虑的另一个选项是Ookii对话库 This is an open source implementation of the OpenFileDialog, and it includes COM wrappers for all the new dialog stuff in Vista. 这是OpenFileDialog的一个开源实现,它包含了Vista中所有新对话框内容的COM包装器。 Using that library you can pop a Vista OpenFileDialog and receive events from the IFileDialogEvents interface, in C# code. 使用该库,您可以弹出Vista OpenFileDialog并使用C#代码从IFileDialogEvents接口接收事件。 One such event is OnFolderChange(). 一个这样的事件是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. 在处理程序中,您可以调用IFolder.GetFolder(),它将为您提供一个IShellItem ,它为您提供用户正在更改的文件夹。 The next step would be to itemize and potentially filter the set of files, which is an exercise I will leave to the reader... 下一步是逐项列出并可能过滤文件集,这是我将留给读者的练习......

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. 但说实话,OpenFileDialog无论如何都不会做很多事情。 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. 说实话,是的,你可能会陷入其中,但是当真正的工作是检查文件的内容然后你可以编写自己的简单的OpenFileDialog类时,你会做很多工作。那。

You might find this question helpful regarding listing contents of zip files: 您可能会发现此问题对于列出zip文件的内容很有帮助:

How to list the contents of a .zip folder in c#? 如何在c#中列出.zip文件夹的内容?

(Note, you could potentially thread it to improve performance, just don't span many threads) (注意,您可以通过线程来提高性能,但不要跨越多个线程)

You can probably use the Windows API Code Pack (comes with the source). 您可以使用Windows API代码包(随源提供)。 The Common File dialogs feature exposes a lot more functionality of file dialogs than the versions in Winforms/WPF. “公用文件”对话框功能比Winforms / WPF中的版本公开了更多的文件对话框功能。

http://code.msdn.microsoft.com/WindowsAPICodePack http://code.msdn.microsoft.com/WindowsAPICodePack

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM