C# get md5,renamed file and can not change file's md5

2022-10-12,,

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();
        }
    }
}

 

《C# get md5,renamed file and can not change file's md5.doc》

下载本文的Word格式文档,以方便收藏与打印。