5

I have multiple extensions in the Filter property of OpenFileDialog. Is possible to hide the extensions and show only the description?

Sample:

dialog.Filter = "Image files|*.bmp;*.jpg; many image file extensions here"

I want to show only the text: "Image files" in the file type combo box because the extension string is very long. Is this possible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

4 Answers4

5

This

dialog.Filter = "Image files (*.bmp)|*.bmp;*.jpg"

will only display "Image files (*.bmp)" in the combo box while still showing files with all the specified extensions.

Or you could do

dialog.Filter = "Image files (*.bmp;...)|*.bmp;*.jpg"

to indicate that it looks for files with extension bmp and some other extensions.

This might depend on the OS. I tested with Windows 7.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • The only decent answer; it's a small trade-off to put something like (*.bmp) or (*.bmp; ...) in the first part of the filter instead of having to accept an XP-style dialog. – Chad Lehman Jan 28 '20 at 19:46
2

This should work:

    dialog.Filter = "All Supported Audio | *.mp3; *.wma | MP3s | *.mp3 | WMAs | *.wma";
    dialog.AutoUpgradeEnabled = false; //using FileDialog.AutoUpgradeEnabled = false it will display the old XP sytle dialog box, which then displays correctly
    dialog.ShowDialog();
T23
  • 582
  • 2
  • 11
  • Not a very good trade-off IMO... getting an older, ghetto-style OpenFileDialog or deal with the long list of file extensions. There should be a better way for Windows 7/8/10+ – Chad Lehman Jan 28 '20 at 19:40
0

It should work exactly as you wrote in your question:

dialog.Filter = "Image files|*.bmp;*.jpeg;*.jpg;*.png;*.gif"
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    No, it doesn't. At least Windows 7 (and probably also Vista) automatically appends "`(*.bmp;*.jpg;other extensions)`" to the displayed text. – Henrik Jan 10 '12 at 10:46
  • Oh, ok - that's not something the documentation states... It also wasn't the case on XP, so: sorry if I got it wrong :-) – Thorsten Dittmar Jan 10 '12 at 13:56
-1

It is very simple, you know. See the following code snippet. It will run perfectly. You can define more file-types like this way.

OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|BMP Files(*.bmp)|*.bmp|GIF Files(*.gif)|*.gif|TIFF Files(*.tiff)|*.tiff|All Files(*.*)|*.*";

There are two parts in the Filter property. "JPG Files(.jpg)|.jpg" means the dropdown for selecting file-types will show "JPG Files(*.jpg)" and the filter will happen against the next part of pipe character i.e. *.jpg.

Note: Never use any space after *.jpg or be it any other file-type. If used, it cannot filter your desired file-type.

.

Kuntal Ghosh
  • 3,548
  • 2
  • 17
  • 21