Limiting the files that can be selected using Open FIle Dialog box
I got this C# windows forms application where I load either a XML file or a CSV file for some task operations. I got a Browse button. When I click the Browse button, 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#?
openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";
this way only csv files or xml files are shown. but 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.