OpenFileDialog始终显示* .URL(Internet快捷方式文件) [英] OpenFileDialog always shows *.URL (Internet Shortcut files)

查看:86
本文介绍了OpenFileDialog始终显示* .URL(Internet快捷方式文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的OpenFileDialog有一个单独的Filter,它也是DefaultExt.我不知道为什么,当对话框打开时,我还会列出所有Internet快捷方式(例如,可以找到文件夹的快捷方式,但不能获取* .URL文件).

My OpenFileDialog has one single Filter which is also the DefaultExt. I wonder why, when the dialog opens, I also get all the Internet Shortcuts listed (it is OK to get the shortcuts to folders, for instance, but not the *.URL files).

是否有一些魔术开关可以打开以便将它们包括在显示给用户的列表中?

Is there some magic switch which I can turn on in order to net get them included in the list displayed to the user?

当前,如果我检测到用户选择了*.URL文件(或者100%的时间都不起作用),我必须通过将e.Cancel设置为true来处理FileOk事件处理程序中的条件. ,某些快捷方式似乎仍在通过FileOk过滤器).不过,不将它们排在第一位会更好.

Currently, I have to handle the condition in the FileOk event handler by setting e.Cancel to true if I detect that the user selected an *.URL file (it is not working 100% of the time either, some shortcuts seem still to be getting through the FileOk filter). Not getting them in the list in the first place would be better, though.

推荐答案

您正确的认为Windows的打开文件"公用对话框显示Internet快捷方式以及文件夹.我不知道为什么会出现这种行为,但是确实存在,并且会在Win32(尝试使用记事本进行验证)以及WinForms应用程序中发生.

You're correct that Windows' Open File common dialogs show Internet Shortcuts along with folders. I have no idea why this behavior exists, but it's there- and it happens in Win32 (try Notepad to verify) as well as WinForms apps.

有一种方法可以解决此问题,但警告:它很hacky!如果从FileDialog类派生自定义文件对话框类,则可以访问一些受保护的事件,这些事件可用于自定义FileDialog操作的各个方面.

There is a way to work around this, but warning: it's hacky! If you derive a custom file dialog class from the FileDialog class, you get access to a few protected events that you can use to customize every aspect of the FileDialog's operation.

Dino Esposito在2003年11月写了《 MSDN杂志》上的一篇文章,展示了该技术的工作原理.本文不再位于MSDN站点上,但您可以在以下位置的Wayback机器的存档中获取它: http://web.archive.org/web/20150117123625/http://msdn.microsoft.com/zh-CN/magazine/cc300434.aspx .

Dino Esposito wrote an MSDN Magazine article in November 2003 that shows how this technique works. This article is no longer on the MSDN site but you can get it on the wayback machine's archive here: http://web.archive.org/web/20150117123625/http://msdn.microsoft.com/en-us/magazine/cc300434.aspx.

您可能需要做的是钩住文件对话框的WndProc或将其子类化,手动浏览文件列表控件,识别为快捷方式的条目,并将Windows消息发送到文件列表控件以删除这些项目. .然后,您需要注意该列表的刷新(例如,从目录更改中刷新)并重复过滤操作.

What you'd probably have to do is to hook or subclass the WndProc of the file dialog, manually look through the file list control, identify entries that were shortcuts, and send Windows messages to the file list control to remove those items. Then you'd need to watch for refreshes of that list (e.g. from a directory change) and repeat the filtering operation.

这将是一个巨大的黑客攻击,但有可能.

This would be a huge hack, but it's possible.

如果这太繁琐或令人讨厌,我建议您仅使用FileOk事件,通过从CancelEventHandler中为FileOk事件返回Cancel=true来防止用户选择快捷方式.

If this is too much work or the hackiness is too much, I'd suggest just using the FileOk event to prevent users from selecting a shortcut by returning Cancel=true from your CancelEventHandler for the FileOk event.

这篇关于OpenFileDialog始终显示* .URL(Internet快捷方式文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆