15

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
user1501034
  • 343
  • 3
  • 6
  • 16
  • why dont you you validate the extension before you upload? – Furqan Hameedi Aug 24 '12 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 Apr 22 '21 at 18:26

3 Answers3

32

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Stephan Schinkel
  • 5,270
  • 1
  • 25
  • 42
  • 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 Aug 24 '12 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.

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
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...