/// <summary> /// Computes an MD5 hash of the contents of a given directory by hashing the union of relative file paths and associated file sizes. /// </summary> public static string ComputeDirectoryHash(string targetPath) { if (!Directory.Exists(targetPath)) { throw new ArgumentException(String.Format("Target directory '{0}' does not exist!", targetPath), "targetPath"); } // Trim all occurrences of standard & alternate directory separator chars and then append a single standard separator to stay consistent. targetPath = targetPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar; // Build a dictionary mapping relative file path to file size. var fileSizeMap = new SortedDictionary <string, long>(); foreach (FileInfo file in DirectoryHelper.GetAllFiles(targetPath)) { string relativePath = file.FullName.Substring(targetPath.Length); fileSizeMap[relativePath] = file.Length; } return(GenerateMD5Hash(fileSizeMap)); }
private static string ComputeDirectoryHash(string targetPath) { // Trim all occurrences of standard & alternate directory separator chars and then append a single standard separator to stay consistent. targetPath = targetPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar; IEnumerable <FileInfo> allFiles = DirectoryHelper.GetAllFiles(targetPath); SortedDictionary <string, long> fileSet = new SortedDictionary <string, long>(); foreach (var file in allFiles) { string relativePath = file.FullName.Substring(targetPath.Length); // Filter out all worker zips and directories, we calculate the logset fingerprint based on contents of the primary only. if (!relativePath.Contains(Path.DirectorySeparatorChar + "worker") || RootIsWorker(relativePath)) { fileSet[relativePath] = file.Length; } } return(GenerateMD5Hash(fileSet)); }