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