C# get folder's Md5 generated by file's and filename's md5. get dictionary md5
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=;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 = ;
static int totalFilesNum = ;
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($"\n\n There are {totalFilesNum} files in {dirFullName},get md5 file number:{fileMd5Num}");
logWriter.WriteLine(logBuilder.ToString());
}
} Console.ReadLine();
} static string GetFolderMD5(string dirFullName)
{
fileMd5Num = ;
totalFilesNum = ;
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=;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 = ; 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 = ; i < fileNameMd5Bytes.Length; i++)
{
fileNameMd5Builder.Append(fileNameMd5Bytes[i].ToString("x2"));
}
}
return fileNameMd5Builder?.ToString();
}
catch
{
return string.Empty;
}
}
}
}
C# get folder's Md5 generated by file's and filename's md5. get dictionary md5的更多相关文章
- An error occurred at line: 1 in the generated java file问题处理
tomcat6启动后,加载jsp页面报错,提示无法将jsp编译为class文件,主要报错信息如下: An error occurred at line: 1 in the generated java ...
- 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...
- python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为
python接口自动化测试二十七:密码MD5加密 ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...
- C#生成MD5码
/// <summary> /// 获取文件的MD5码 /// </summary> /// <param name="fileName">传入 ...
- unity下载资源存储-生成md5
IEnumerator GetText() { using (UnityWebRequest request = UnityWebRequest.Get("localhost:80/txt/ ...
- C#获取文件的MD5码
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Send ...
- 获取文件的MD5码(C#)
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Test ...
- 在.NET中计算文件的MD5值
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月2日. 直接上代码吧: using System; using System.IO; using System.Security. ...
- 十八. Python基础(18)常用模块
十八. Python基础(18)常用模块 1 ● 常用模块及其用途 collections模块: 一些扩展的数据类型→Counter, deque, defaultdict, namedtuple, ...
随机推荐
- 比特宇宙-TCP/IP的诞生
前情回顾: 我是一个explorer的线程 我是一个杀毒软件线程 我是一个IE浏览器线程 比特宇宙创世不久,宇宙中开始出现文明. 文明不断碰撞.融合.进化,逐渐分化为几大派系: 施行Unix体制的帝国 ...
- vue中computed(计算属性)和watch在实现父子组件props同步时的实际区分
vue中computed和watch的对比是一个很有意思的话题. 看过官网教程以后,我们往往更倾向多使用computed.computed优点很多,却在某些时候不太适用. 今天我们就稍微讨论一下,当我 ...
- postgresql,postgis,geoserver 发布地图服务,并用.net mvc openlayers3进行显示
1.所需工具 postgres版本 9.6.1 对应的postgis geoserver 2.8.2 openlayers3 2.将postgres postgis ,geosever安装好,再用如下 ...
- [Mathematics][BJTU][Calculus]Detailed explanations and proofs of the Dirac-Abel Discriminant Methods which deal with the conditional convergence
So, today we will talk about the conditional convergence and two discriminant methods, namely Dirac- ...
- php mysql_connect 在同一host下多数据库mysql_select_db()的bug .
操作方法 创建两个数据库test1 test2 同一个host下面 分别在两个数据库中创建表 -- ---------------------------- -- Table structure fo ...
- luogu P2860 [USACO06JAN]冗余路径Redundant Paths |Tarjan
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- go proxy athens 部署到k8s
目录 go proxy athens 部署到k8s 一.athens简介 二.部署 1.创建 PersistentVolume 2.创建service 3.创建deployment 4.ci/cd中使 ...
- 谈架构设计中DDD思想的运用
首先,描述一下我的业务场景及项目分层结构,非标准DDD(其实我不觉得有标准),只是思考的时候有带入DDD思想. 业务场景:这是一个ERP系统对中台提供的接口项目,仓储操作大多都是存储过程去完成的. 项 ...
- HDU1079 Calender Game
Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they pl ...
- POJ 1949 Chores
Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as p ...