I need to read from a number of files but sometimes they are not available or cannot be read from current user. This is the code I'm using to check if file is valid but I think I'm not using the using statement correctly.
private static bool fileAccessible(string filePath) {
using (FileStream fs = new FileStream(filePath, FileMode.Open)) {
if (fs.CanRead) {
return true;
}
else {
return false;
}
}
return false; // compiler says "unreachable code"
}
Should I insert a try/catch block outside the using statement to catch errors raised by unavailable file or will using go on without raising an error if filePath is not available?
Bonus question: is this the correct way to check if file is available and accessible? If the answer is Yes can I change FileMode.Open to FileMode.Append to perform the same check on writable files?