简体   繁体   English

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

[英]Excluding file extensions from open file dialog in C#

I am trying to put a filter on my C# openFileDialog that excludes certain file extensions.我正在尝试在我的 C# openFileDialog 上放置一个过滤器,以排除某些文件扩展名。 For example I want it to show all files in a directory that are not .txt files.例如,我希望它显示目录中不是 .txt 文件的所有文件。

Is there a way to do this?有没有办法做到这一点?

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?想想看,你有没有在 Windows 中看到一个打开文件对话框有这个? 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.最好的办法是让他们选择所有文件,然后提示用户是否选择了不允许的文件,或者将其过滤为您可以处理的所有可能文件。

There is no direct way to do this using the BCL OpenFileDialog.使用 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. 1) 制作一个只包含您想要支持的所有类型的过滤器。 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 . 2) 使用与此自定义 OpenFileDialog 实现类似的内容。 You could then override the OnFileNameChanged() method to potentially disable the "Open" button if the selected file has a .txt extension.如果所选文件具有 .txt 扩展名,则您可以覆盖 OnFileNameChanged() 方法以潜在地禁用“打开”按钮。

3) Let the user pick a .txt file, throw up an error dialog, and reopen the file dialog. 3) 让用户选择一个 .txt 文件,抛出一个错误对话框,然后重新打开文件对话框。 This feels clunky and not too great to me, though....不过,这对我来说感觉很笨重而且不太好......

Found with Google search "OpenFileDialog" 通过 Google 搜索“OpenFileDialog”找到

EDIT: Sorry about not expanding on the EXCLUDE aspects.编辑:很抱歉没有扩展 EXCLUDE 方面。 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) (System.IO.Path.GetExtension() 和 Linq 的 .Distint() 方法可能在这里工作得很好)
  • Remove the ".txt" entry from the list.从列表中删除“.txt”条目。
  • Construct a filter string by looping through these extensions.通过循环遍历这些扩展来构建过滤器字符串。

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 a) 包括一长串应该允许的所有扩展名,或

  • b) Allow all extensions, and then use a simple check in code that reopens the dialog if the selected file has extension .txt . b) 允许所有扩展名,然后使用简单的签入代码,如果所选文件的扩展名是.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.在几乎所有情况下,您可能无法仅使用过滤器(可能还有一些代码检查)就可以逃脱。

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.不使用 OpenFileDialog,而是分析您需要的特定功能并创建您自己的对话框。 You could easily provide whatever filtering you want, but then the difficulty moves over to the implementing and maintaining the new UI.您可以轻松地提供您想要的任何过滤,但随后困难就转移到了实现和维护新 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.这可能是不可能的,但是我们对该应用程序了解不多。

You cannot set a filter to exclude extensions from file dialogs.您不能设置过滤器以从文件对话框中排除扩展名。

You could however implement a delegate for the FileOk event on the dialog.但是,您可以在对话框上为FileOk事件实现一个委托。 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.此事件在用户选择的文件被接受之前触发,事件参数提供了一个Cancel属性,您可以将其设置为禁止选择。

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. PS:不要忘记给用户反馈为什么文件不被接受,否则他们可能想知道为什么在选择“txt”文件时对话框没有关闭。

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

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