My application traverses a directory tree and in each directory it tries to open a file with a particular name (using File.OpenRead()). If this call throws FileNotFoundException then it knows that the file does not exist. Would I rather have a File.Exists() call before that to check if file exists? Would this be more efficient?
My application traverses a directory tree and in each directory it tries to open
Share
It depends !
If there’s a high chance for the file to be there (you know this for your scenario, but as an example something like
desktop.ini) I would rather prefer to directly try to open it.Anyway, in case of using
File.Existyou need to putFile.OpenReadin try/catch for concurrency reasons and avoiding any run-time exception but it would considerably boost your application performance if the chance for file to be there is low. Ostrich algorithm