从 C# 中的打开文件对话框中排除文件扩展名

发布于 2024-07-22 04:17:40 字数 105 浏览 11 评论 0原文

我正在尝试在 C# openFileDialog 上放置一个过滤器,以排除某些文件扩展名。 例如,我希望它显示目录中不是 .txt 文件的所有文件。

有没有办法做到这一点?

I am trying to put a filter on my C# openFileDialog that excludes certain file extensions. For example I want it to show all files in a directory that are not .txt files.

Is there a way to do this?

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

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

发布评论

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

评论(6

荒岛晴空 2024-07-29 04:17:41

没有直接的方法可以使用 BCL OpenFileDialog 来执行此操作。

我可以想到几个选项:

1)制作一个仅包含您想要支持的所有类型的过滤器。 这是我的建议,因为这是进行此类操作的最常见方法。

2)使用类似于此的内容 自定义 OpenFileDialog 实现。 然后,如果所选文件具有 .txt 扩展名,您可以重写 OnFileNameChanged() 方法以潜在地禁用“打开”按钮。

3) 让用户选择一个 .txt 文件,弹出错误对话框,然后重新打开文件对话框。 不过,这感觉很笨重,对我来说不太好......

There is no direct way to do this using the BCL OpenFileDialog.

I can think of a couple of options:

1) Make a filter that just has all of the types you do want to support. This would be my recommendation, since that's the most common way of going about this type of operation.

2) Use something along the lines of this custom OpenFileDialog implementation. You could then override the OnFileNameChanged() method to potentially disable the "Open" button if the selected file has a .txt extension.

3) Let the user pick a .txt file, throw up an error dialog, and reopen the file dialog. This feels clunky and not too great to me, though....

心如荒岛 2024-07-29 04:17:41

通过 Google 搜索“OpenFileDialog”找到

编辑:抱歉没有扩展排除方面。 您可能不需要走到这个极端,但它会满足您的需求......

  • 对用户可能选择的所有文件进行递归目录搜索。 (希望这是一小组文件夹。)
  • 唯一标识这些文件的扩展名。 (System.IO.Path.GetExtension() 和 Linq 的 .Distint() 方法在这里可能效果很好)
  • 从列表中删除“.txt”条目。
  • 通过循环这些扩展来构造过滤器字符串。

Found with Google search "OpenFileDialog"

EDIT: Sorry about not expanding on the EXCLUDE aspects. You may not need to go to this extreme, but it would meet your needs....

  • Do a recursive directory search of all the files that the user may choose from. (Hopefully that's a small set of folders.)
  • Uniquely identify the extensions on those files. (System.IO.Path.GetExtension() and Linq's .Distint() method may work well here)
  • Remove the ".txt" entry from the list.
  • Construct a filter string by looping through these extensions.
眸中客 2024-07-29 04:17:41

恐怕这是不可能的。 您必须

  • a) 包含应允许的所有扩展的长列表,或

  • b)允许所有扩展名,然后使用简单的签入代码,如果所选文件具有扩展名 .txt,则重新打开对话框。

另外,您能为这个问题提供更多背景信息吗? 我无法想象可能明确需要排除某个扩展的场景。 在几乎所有情况下,您可能无法只使用一个过滤器(也许还需要检查一下代码)。

I'm afraid this isn't possible. You'll either have to

  • a) Include a long list of all the extensions that should be allowed, or

  • b) Allow all extensions, and then use a simple check in code that reopens the dialog if the selected file has extension .txt.

Also, could you provide a bit more context for this question? I'm having trouble envisaging a scenario where I might explicitly need to exclude a certain extension. You can't probably get away with just a filter (and maybe a bit of checking in code) in almost all situations.

苹果你个爱泡泡 2024-07-29 04:17:41

这是一种完全不同的方法,您不太可能使用它。

不要使用 OpenFileDialog,而是分析您需要的特定功能并创建您自己的对话框。 您可以轻松地提供您想要的任何过滤,但随后困难就会转移到实施和维护新的 UI 上。

我不建议您这样做,但有时用户对他们的需求相当坚持。

另一种选择:不使用对话框,而是想出一些完全不同的、适合应用程序的东西。 这可能是不可能的,但我们对这个应用程序了解不多。

Here's a completely different approach, which you're unlikely to use.

Instead of using OpenFileDialog, analyze the specific features you need and create your own dialog box. You could easily provide whatever filtering you want, but then the difficulty moves over to the implementing and maintaining the new UI.

I don't suggest that you do this, but sometimes users are rather insistent upon what they need.

Another alternate: Instead of using a dialog, come up with something completely different that fits well within the app. This may not be possible, but then we don't know much about the app.

只怪假的太真实 2024-07-29 04:17:41

您无法设置过滤器来从文件对话框中排除扩展名。

不过,您可以在对话框上实现 FileOk 事件的委托。 此事件在用户选择的文件被接受之前触发,并且事件参数提供一个 Cancel 属性,您可以将其设置为禁止选择。

它并不像实际使错误的文件不可见那么优雅,但它允许您禁止选择错误类型的文件。

PS:不要忘记向用户反馈文件未被接受的原因,否则他们可能会想知道为什么当他们选择“txt”文件时对话框没有关闭。

You cannot set a filter to exclude extensions from file dialogs.

You could however implement a delegate for the FileOk event on the dialog. This event fires just before the file the user selected will be accepted, and the event arguments provide a Cancel property that you can set to disallow selection.

It is not as elegant as actually making the wrong files invisible, but it will allow you to disallow selection of the wrong kind of file.

PS: Do not forget to give the user feedback why the file was not accepted, otherwise they may wonder why the dialog is not closing when they pick a 'txt' file.

日久见人心 2024-07-29 04:17:40

我认为这是不可能的。 过滤器的设置方式是您可以选择要显示的文件,但我认为没有办法显示“除...之外的所有文件”。 想一想,您见过 Windows 中的打开文件对话框吗? 我想我从来没有见过一个。

最好的办法是让他们选择所有文件,然后提示用户是否选择了不允许的文件,或者将其过滤到您可以处理的所有可能的文件。

I don't think this is possible. The way the filter is set up, is that you can choose which files to show, but I don't think there's a way to show "All files except...". Come to think of it, have you ever seen an Open File Dialog in Windows that has this? I don't think I've ever seen one.

Your best bet is to let them choose all files, and then prompt the user if they select one that isn't allowed OR filter it down to all the possible files that you can deal with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文