C# get md5,renamed file and can not change file's md5
程序员文章站
2022-04-14 19:38:14
using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace ConsoleApplication13 { class Program { static void Mai... ......
using system; using system.text; using system.io; using system.security.cryptography; namespace consoleapplication13 { class program { static void main(string[] args) { string imgpath1 = @"..\..\images\lj.jpg"; string imgpath2 = @"..\..\images\lj2.jpg"; string imgpath3 = @"..\..\images\lj3.jpg"; string imgpath4 = @"..\..\images\lj4.jpg"; string md51 = getmd5(imgpath1); string md52 = getmd5(imgpath2); string md53 = getmd5(imgpath3); string md54 = getmd5(imgpath4); console.writeline($"path:{imgpath1},md51:{md51}"); console.writeline($"path:{imgpath2},md52:{md52}"); console.writeline($"path:{imgpath3},md53:{md53}"); console.writeline($"path:{imgpath4},md54:{md54}"); console.readline(); } static string getmd5(string sourcefile) { stringbuilder md5builder = new stringbuilder(); if (file.exists(sourcefile)) { using (md5 md5hash = md5.create()) { using(filestream fs=file.open(sourcefile,filemode.open)) { byte[] md5bytes = md5hash.computehash(fs); for (int i = 0; i < md5bytes.length; i++) { string sortedbyte = md5bytes[i].tostring("x2"); if (!string.isnullorempty(sortedbyte)) { md5builder.append(sortedbyte); } } } } } return md5builder.tostring(); } } }