16

I have a C# Windows Forms application where I load either a XML file or a CSV file for some task operations. When I click the Browse button I have, an Open File Dialog box appears and I can navigate to a location on my drive and choose the file and then upload it using an Upload button.

If I load a JPG or a ZIP file or any file whose format is anything except CSV or XML, my application crashes. Is there any way of limiting the Open File Dialog box to open only CSV or XML files alone in C#?

2
  • why dont you you validate the extension before you upload? Commented Aug 24, 2012 at 11:35
  • @FurqanHameedi This is the better way to do exactly that. Not only does it work to prevent users from selecting the wrong file type in the dialog window, but it also doesn't mislead users into thinking it's fine by letting them pick a wrong filetype, only to crash upon submission.
    – TylerH
    Commented Apr 22, 2021 at 18:26

3 Answers 3

33

Use

openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";

this way only csv files or xml files are shown.

Odd-numbered pipes delineate between what's visible in the Filter dropdown and the corresponding actual file extension, and Even-numbered pipes delineate between the first entire file extension and the second.

For example, "CSV files (*.csv)|*csv" means users will see "CSV files (*.csv)" in the filter dropdown, and that option will look for any files that match *.csv.

In the line of code above, the pipe before "XML" indicates an entirely new filter option that will appear below the CSV option.

Nevertheless, users can also select other filetypes if they type in the complete name - so check the filename that was selected and correct your code accordingly.

1
  • 4
    You can also combine filters so the user can see both csv and xml files, like this: openFileDialog.Filter = "Data files |*.csv;*.xml";
    – sventevit
    Commented Aug 24, 2012 at 13:15
1

You can use the Filter property to let the user choose a certain type of file.

However! This is not a guarantee. A user is still able to input '(star).(star)' in the filename box and show all files. So you should check the resulting file(s) in your code as well.

You can do this with the Path.GetExtension() method.

0

You can apply a filter in your Open file dialog which only shows .xml and csv files as mentioned above. With path.getextension http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx You can check if the user indeed selected a file with the right extension. If a wrong extension is selected, you can prompt to select a different file.

I would strongly recommend to check the file extension before upload. Just check the extension after the user had selected the file. If the wrong files was selected, just don't continue the upload/processing...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.