5

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?

6 Answers 6

4

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.

1
  • Okay, I was afraid this was the case. Just wondered if there was a way to do it. Thanks for the answer and actually reading my question.
    – user113872
    May 28, 2009 at 16:53
2

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

2
  • Option 1 sounds best. The real issue the users of my software have asked that .txt files within the directory not even show up, because they would never choose a .txt file to open in this case. The .txt files just clutter the list of files through which they have to look.
    – user113872
    May 28, 2009 at 16:56
  • @ZGray: That would be my recommendation. It's the normal way of working in Windows. May 28, 2009 at 17:26
1

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.
8
  • The question was if it's possible to EXCLUDE certain files. You're answer shows how to filter to one specific file. The OP wants all files except txt.
    – BFree
    May 28, 2009 at 16:49
  • No that does not answer my question at all. I understand how a filter works. I want to EXCLUDE all .txt files not include .txt files.
    – user113872
    May 28, 2009 at 16:51
  • It certainly IS possible using this filter. The idea I should have mentioned is that the developer may need to do a directory search to find all available extensions and include them in the filter, but not including the *.txt in the filter. May 28, 2009 at 16:52
  • Yes, but an OpenFileDialog allows the user to browse to ANY folder. You need to set the filter before showing it, so yes, you may be able to filter out the ones in the directory where you're opening the dialog, but once the user navigates to a different folder, you'll be missing many files in your filter.
    – BFree
    May 28, 2009 at 16:57
  • BFree, my comment mentioned that they "may" be able to isolate to a specific folder. If not, then a background process checking all extensions on the system may be needed. (Of course, the other poster brought up the truly relevant question of why the design must be this way.) May 28, 2009 at 17:05
0

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.

1
  • In this scenario I just want the user to not have to deal with the .txt files cluttering the list of files.
    – user113872
    May 28, 2009 at 16:58
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.