C# get dictionary md5
static string GetDicMD5(string dirFullName) { logBuilder = new StringBuilder(); Dictionary<string, string> folderMd5Dic = GetFolderFilesAndNameMD5Dic(dirFullName); if (folderMd5Dic == null && folderMd5Dic.Any()) { return null; } using (MemoryStream ms = new MemoryStream()) { using (MD5 folderDicMd5 = MD5.Create()) { StringBuilder folderDicBuilder = new StringBuilder(); BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize(ms, folderMd5Dic); byte[] folderDicMd5Bytes = folderDicMd5.ComputeHash(ms); if(folderDicMd5Bytes!=null && folderDicMd5Bytes.Any()) { for(int i=0;i<folderDicMd5Bytes.Length;i++) { folderDicBuilder.Append(folderDicMd5Bytes[i].ToString("x2")); } } return folderDicBuilder.ToString(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace FolderMd5NS { class Program { static StringBuilder logBuilder=null; static int fileMd5Num = 0; static int totalFilesNum = 0; static void Main(string[] args) { string dirFullName = @"D:C"; try { string currentoFolderMd5 = GetFolderMD5(dirFullName); Console.WriteLine(currentoFolderMd5); } catch { } finally { using (StreamWriter logWriter = new StreamWriter($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}log.txt", true)) { logBuilder.AppendLine($" There are {totalFilesNum} files in {dirFullName},get md5 file number:{fileMd5Num}"); logWriter.WriteLine(logBuilder.ToString()); } } Console.ReadLine(); } static string GetFolderMD5(string dirFullName) { fileMd5Num = 0; totalFilesNum = 0; logBuilder = new StringBuilder(); Dictionary<string, string> folderMd5Dic = GetFolderFilesAndNameMD5Dic(dirFullName); if (folderMd5Dic == null && folderMd5Dic.Any()) { return null; } using (MemoryStream ms = new MemoryStream()) { using (MD5 folderDicMd5 = MD5.Create()) { StringBuilder folderDicBuilder = new StringBuilder(); BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize(ms, folderMd5Dic); byte[] folderDicMd5Bytes = folderDicMd5.ComputeHash(ms); if(folderDicMd5Bytes!=null && folderDicMd5Bytes.Any()) { for(int i=0;i<folderDicMd5Bytes.Length;i++) { folderDicBuilder.Append(folderDicMd5Bytes[i].ToString("x2")); } } return folderDicBuilder.ToString(); } } } static Dictionary<string,string> GetFolderFilesAndNameMD5Dic(string dirFullName) { if (!Directory.Exists(dirFullName)) { return null; } string[] allFilesInFolder = Directory.GetFiles(dirFullName, "*", SearchOption.AllDirectories); totalFilesNum = allFilesInFolder.Count(); if (allFilesInFolder == null && !allFilesInFolder.Any()) { return null; } Dictionary<string, string> folderMd5CheckDic = new Dictionary<string, string>(); GetDirAllFilesMd5ToDic(allFilesInFolder, folderMd5CheckDic); return folderMd5CheckDic; } private static void GetDirAllFilesMd5ToDic(string[] allFilesInFolder, Dictionary<string, string> folderMd5CheckDic) { try { foreach (var file in allFilesInFolder) { string fileMd5 = GetSingleFileMD5(file); string fileNameMd5 = GetFileNameMD5(file); string dicKey = fileMd5 + fileNameMd5; folderMd5CheckDic.Add(dicKey, file); string msg = $"FileName:{file}, file md5:{fileMd5},num:{fileMd5Num++}"; Console.WriteLine(msg); logBuilder.AppendLine(msg); } } catch { } } static string GetSingleFileMD5(string fileFullName) { if (!File.Exists(fileFullName)) { return string.Empty; } StringBuilder md5Builder = new StringBuilder(); try { using (FileStream fs = File.Open(fileFullName, FileMode.Open)) { using (MD5 md5 = MD5.Create()) { byte[] md5Bytes = md5.ComputeHash(fs); for (int i = 0; i < md5Bytes.Length; i++) { md5Builder.Append(md5Bytes[i].ToString("x2")); } } } return md5Builder?.ToString(); } catch { return string.Empty; } } static string GetFileNameMD5(string fileFullName) { if (!File.Exists(fileFullName) || string.IsNullOrEmpty(fileFullName)) { return string.Empty; } StringBuilder fileNameMd5Builder = new StringBuilder(); try { byte[] fileNameBytes = Encoding.UTF8.GetBytes(fileFullName); using (MD5 fileNameMd5 = MD5.Create()) { byte[] fileNameMd5Bytes = fileNameMd5.ComputeHash(fileNameBytes); for (int i = 0; i < fileNameMd5Bytes.Length; i++) { fileNameMd5Builder.Append(fileNameMd5Bytes[i].ToString("x2")); } } return fileNameMd5Builder?.ToString(); } catch { return string.Empty; } } } }