I have the following C# code for calculating each file's hash in a certain, user specified directory. The point is that it works fine, until it encounters a file that it cannot access. When it finds something like this, it just throws an error message and exits the program. What I want it instead to do is, throw an error message with the name of the file that cannot be accessed, write that there is an error in accessing that file, and continue executing the program with the other files in the directory. If someone can help me edit my code and achieve those things I would be glad.
private void SHA256Directory(string directory) { try { SHA256 DirectorySHA256 = SHA256Managed.Create(); byte[] hashValue; DirectoryInfo dir = new DirectoryInfo(directory); FileInfo[] files = dir.GetFiles(); foreach (FileInfo fInfo in files) { FileStream fStream = fInfo.Open(FileMode.Open); fStream.Position = 0; hashValue = DirectorySHA256.ComputeHash(fStream); Console.WriteLine(fInfo.Name); Miscellaneous.ByteArrayToHex(hashValue); Miscellaneous.ByteArrayToBase64(hashValue); Console.WriteLine(); fStream.Close(); } return; } catch(DirectoryNotFoundException) { Console.WriteLine("Error: The directory specified could not be found."); } catch(IOException) { Console.WriteLine("Error: A file in the directory could not be accessed."); } catch(ArgumentNullException) { Console.WriteLine("Error: The argument cannot be null or empty."); } }