public static void Run() { bool flag = true; while (flag) { Console.WriteLine("Select an action!"); Console.WriteLine(); Console.WriteLine("Press one to read the file hash"); Console.WriteLine("Press two to save hash to file"); Console.WriteLine("Press three to compare with the saved file hash"); Console.WriteLine(); Console.WriteLine("To exit from the program, press zero."); try { int value = Convert.ToInt32(Console.ReadLine()); switch (value) { case 1: Console.WriteLine("Please enter the file path!"); string filePathRead = Console.ReadLine(); ReadHash.ReadFileHash(filePathRead); Console.WriteLine(new string('-', 70)); break; case 2: Console.WriteLine("Please enter the file path!"); string filePathWrite = Console.ReadLine(); WriteHash.WriteHashFile(filePathWrite); Console.WriteLine(new string('-', 70)); break; case 3: Console.WriteLine("Please enter the file path!"); string filePathCompare = Console.ReadLine(); HashComparison.Comparison(filePathCompare); Console.WriteLine(new string('-', 70)); break; case 0: flag = false; break; } } catch { Console.WriteLine("You have entered a wrong value. Please try again!"); Console.WriteLine(new string('-', 70)); Console.WriteLine(); } } }
public static string WriteHashFile(string filePath) { var fileInfo = new FileInfo(filePath); if (fileInfo.Exists) { string hashvalue = ReadHash.ReadFileHash(filePath); FileStream fileStream = fileInfo.Open(FileMode.OpenOrCreate); var fileHash = new FileStream(pathToHash, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); var write = new StreamWriter(fileHash); write.Write(hashvalue); write.Close(); fileStream.Close(); Console.WriteLine($"Open file {fileInfo.FullName} and save hash in file {new FileInfo(pathToHash).FullName}"); return(hashvalue); } else { return("Wrong file path !!!"); } }
public static void Comparison(string filePath) { string fileHashValue = ReadHash.ReadFileHash(filePath); string saveFileHashValue = ReadSaveHash.ReadHash(); if (saveFileHashValue != null) { bool result = Equals(fileHashValue, saveFileHashValue); switch (result) { case true: Console.WriteLine("Hashes are equal!"); break; case false: Console.WriteLine("Hashes are not equal!"); break; } } else { Console.WriteLine("write file hash and try again!!!"); } }