Whether you can read the file depends on a number of factors: do you have permissions, is the hard disk damaged. I would probably go the same way as you.
However, you need to keep in mind that the information you get from this method is just a snapshot. If, right after calling this method, someone changes the file permissions, access to the file later in your code will still fail. You should not depend on the result of this method.
Just a suggestion, the following code does the same, but a little more concise:
try { File.Open(this.DataSourceFileName, FileMode.Open, FileAccess.Read).Dispose(); return true; } catch (IOException) { return false; }
Since you are not actually using a stream, you do not need to keep a link to it. Instead, you can immediately get rid of the stream by calling dispose on the result of File.Open() .
EDIT:
See https://gist.github.com/pvginkel/56658191c6bf7dac23b3893fa59a35e8 for an explanation of why I put Dispose() at the end of File.Open() instead of using the using statement.
Pieter van ginkel
source share