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?

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!


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...


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)


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

链接地址: http://www.djcxy.com/p/42658.html

上一篇: 参考通用评论

下一篇: 基于内容过滤OpenFileDialog中显示的文件?