tags:

views:

24

answers:

1

Can I count on the Common File Dialogs eg. OpenFileDialog, SaveFileDialog and their properties eg. Filter, CheckFileExists, CheckPathExists to give me a valid file? Or must I check again?

I guess if I were to check again, I will use System.IO.Path.GetExtension? I wonder what happens if an invalid file is provided (eg. rename a .txt to .exe)

A: 

You can be reasonably sure that, with the settings you are talking about, the returned file will exist with the name given at the time the file was selected; however, there's nothing to prevent that file from being deleted, for example, between its selection and when your code takes action with it.

What happens if an invalid file is selected depends entirely on your code; ideally, you should check everything appropriate (file exists or not, is of the correct type, is not in a 'dangerous' location) and display proper error messages before your code does anything.

Andrew Barber
So how would you validate the file data is correct? eg. not a exe renamed a jpg for example.
jiewmeng
Validating that the contents of the file is totally dependent on what kind of file you expect to have, but basically involves learning enough about the expected format of the file so that you can validate it. You can often use existing libraries to test a file format by trying to load the file; for example, initialize a System.Drawing.Bitmap object with a file to test if it's a valid image.
Andrew Barber