OpenFileDialog 始终显示 *.URL(Internet 快捷方式文件)

发布于 2024-08-15 00:35:50 字数 423 浏览 2 评论 0原文

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

是否有一些我可以打开的神奇开关,以便将它们包含在向用户显示的列表中?

目前,如果我检测到用户选择了 e.Cancel ,我必须通过将 e.Cancel 设置为 true 来处理 FileOk 事件处理程序中的条件code>*.URL 文件(它也不是 100% 正常工作,一些快捷方式似乎仍在通过 FileOk 过滤器)。不过,一开始就不把它们列入名单会更好。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-08-22 00:35:51

您是正确的,Windows 的“打开文件”常用对话框显示 Internet 快捷方式以及文件夹。我不知道为什么存在这种行为,但它确实存在 - 并且它发生在 Win32(尝试记事本来验证)以及 WinForms 应用程序中。

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

Dino Esposito 在 2003 年 11 月撰写了一篇 MSDN 杂志文章,展示了该技术的工作原理。本文不再出现在 MSDN 站点上,但您可以在此处的 wayback machine 存档中获取它: http://web.archive.org/web/20150117123625/http://msdn.microsoft.com/en-us/magazine/cc300434.aspx

您可能需要做的是挂钩文件对话框的 WndProc 或对其进行子类化,手动查看文件列表控件,识别快捷方式条目,并将 Windows 消息发送到文件列表控件以删除这些项目。然后,您需要监视该列表的刷新(例如,目录更改)并重复过滤操作。

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

如果这工作量太大或者黑客行为太多,我建议只使用 FileOk 事件来阻止用户通过从 FileOk 事件的 CancelEventHandler 返回 Cancel=true 来选择快捷方式。

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.

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

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.

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.

水波映月 2024-08-22 00:35:51

恼人的。您可以通过实现 FileOk 事件的处理程序来攻击它们,这样用户就永远无法选择一个:

private void openFileDialog1_FileOk(object sender, CancelEventArgs e) {
  string ext = System.IO.Path.GetExtension(openFileDialog1.FileName);
  if (String.Compare(ext, ".url", true) == 0) e.Cancel = true;
}

Annoying. You can whack them by implementing a handler for the FileOk event so the user can never select one:

private void openFileDialog1_FileOk(object sender, CancelEventArgs e) {
  string ext = System.IO.Path.GetExtension(openFileDialog1.FileName);
  if (String.Compare(ext, ".url", true) == 0) e.Cancel = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文