using System; using System.IO; using System.Linq; using System.Text; using System.Security; using System.Configuration; using System.Security.Principal; using System.Collections.Generic; using System.Security.Cryptography; namespace FileComparer { class Program { static void Main(string[] args) { try { /*Validating the folder names*/ if (!Directory.Exists(ConfigurationManager.AppSettings["folder1"]) || !Directory.Exists(ConfigurationManager.AppSettings["folder2"])) throw new Exception("Folder(s) do not exist!"); else { /*Load the Dictionaries*/ Diff.loadDictionaries(ConfigurationManager.AppSettings["folder1"], Diff.table1); Diff.loadDictionaries(ConfigurationManager.AppSettings["folder2"], Diff.table2); /*Compare & combine the data*/ Diff.processItems(); /*Print information*/ Diff.printData(); } } catch (Exception e) { Console.WriteLine(e.Message + "\n\nException Message>> " + e + "\n" + e.Source + "\n" + e.StackTrace + "\n" + e.InnerException); } finally { Console.WriteLine("Press any key to continue..."); Console.ReadLine(); } } } class Diff { public static Dictionary table1 = null, table2 = null; private static HashSet set = null; private static String origPath = null; private static MD5 md5 = null; static Diff() { table1 = new Dictionary(); table2 = new Dictionary(); md5 = new MD5CryptoServiceProvider(); set = new HashSet(); origPath = string.Empty; } public static void loadDictionaries(String path, Dictionary dict) { try { origPath = path; dfs(path, dict); } catch(Exception e) { throw e; } } public static void dfs(String path, Dictionary dict) { foreach (var x in Directory.GetFiles(path)) using (FileStream file = new FileStream(x, FileMode.Open)) { byte[] retVal = md5.ComputeHash(file); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) sb.Append(retVal[i].ToString("x2")); dict.Add(x.Replace(origPath, string.Empty), sb.ToString()); } foreach (var y in Directory.GetDirectories(path)) dfs(y, dict); } public static void processItems() { HashSet common = new HashSet(table1.Keys.Intersect(table2.Keys)); foreach (var x in table1.Keys) if (common.Contains(x)) { if (table1[x] != table2[x]) set.Add(x); } else set.Add(x); table1 = null; foreach(var y in table2.Keys) if (!common.Contains(y)) set.Add(y); table2 = null; } public static void printData() { if (set.Count > 0) { Console.WriteLine("Items include: "); foreach (var x in set) Console.WriteLine(x); Console.WriteLine("\nTotal number of changed files: " + set.Count); } else Console.WriteLine("No changes noticed"); } } }