首页 > 代码库 > C#对文件进行MD5值检测

C#对文件进行MD5值检测

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;  6 using System.ComponentModel;  7 using System.Data;  8 using System.Drawing;  9 using System.IO; 10 using System.Security.Cryptography; 11 using System.Diagnostics; 12 using System.ServiceProcess; 13  14 namespace org.nipc.securityManager.client.UpdateModule 15 { 16     public  class MD5Check 17     { 18         //static void Main(string[] args) 19         //{ 20             //string strMD5 = MD5File(@"C:\Users\Administrator\Desktop\my.docx");//读取升级包的MD5值 21             //Console.WriteLine(strMD5); 22             //Console.ReadKey(); 23  24             //Process[] ps = Process.GetProcesses();//停止老版本正在运行的进程 25             //foreach (Process item in ps) 26             //{ 27             //    if (item.ProcessName == "SecurityManagerClient") 28             //    { 29             //        item.Kill(); 30             //    } 31             //} 32  33             //object[] o = System.ServiceProcess.ServiceController.GetServices(); 34             //for (int i = 0; i < o.Length; i++) 35             //{ 36             //    ((ServiceController[])o)[i].Start(); //启动服务 37             //    bool stopYN = ((ServiceController[])o)[i].CanStop; //判断服务是否可以停止 38             //    if (stopYN) 39             //        ((ServiceController[])o)[i].Stop();//停止服务 40             //} 41  42             ////执行老软件的卸载程序 43             //System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\SMSetup1.0.0.7.exe");//路径有待修改 44  45             ////执行新软件的安装程序 46             //System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\SMSetup1.0.0.7.exe");//路径有待修改 47         //} 48         /// <summary> 49         /// 计算文件的 MD5 值 50         /// </summary> 51         /// <param name="fileName">要计算 MD5 值的文件名和路径</param> 52         /// <returns>MD5 值16进制字符串</returns> 53         public static string MD5File(string fileName) 54         { 55             return HashFile(fileName, "md5"); 56         } 57  58         /// <summary> 59         /// 计算文件的哈希值 60         /// </summary> 61         /// <param name="fileName">要计算哈希值的文件名和路径</param> 62         /// <param name="algName">算法:sha1,md5</param> 63         /// <returns>哈希值16进制字符串</returns> 64         public static string HashFile(string fileName, string algName) 65         { 66             if (!System.IO.File.Exists(fileName)) 67                 return string.Empty; 68  69             FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 70             byte[] hashBytes = HashData(fs, algName); 71             fs.Close(); 72             return ByteArrayToHexString(hashBytes); 73         } 74  75         /// <summary> 76         /// 计算哈希值 77         /// </summary> 78         /// <param name="stream">要计算哈希值的 Stream</param> 79         /// <param name="algName">算法:sha1,md5</param> 80         /// <returns>哈希值字节数组</returns> 81         public static byte[] HashData(Stream stream, string algName) 82         { 83             HashAlgorithm algorithm; 84             if (algName == null) 85             { 86                 throw new ArgumentNullException("algName 不能为 null"); 87             } 88             if (string.Compare(algName, "sha1", true) == 0) 89             { 90                 algorithm = SHA1.Create(); 91             } 92             else 93             { 94                 if (string.Compare(algName, "md5", true) != 0) 95                 { 96                     throw new Exception("algName 只能使用 sha1 或 md5"); 97                 } 98                 algorithm = MD5.Create(); 99             }100             return algorithm.ComputeHash(stream);101         }102 103         /// <summary>104         /// 字节数组转换为16进制表示的字符串105         /// </summary>106         /// <param name="buf"></param>107         /// <returns></returns>108         public static string ByteArrayToHexString(byte[] buf)109         {110             string returnStr = "";111             if (buf != null)112             {113                 for (int i = 0; i < buf.Length; i++)114                 {115                     returnStr += buf[i].ToString("X2");116                 }117             }118             return returnStr;119 120         }121 122     }123 }

 

C#对文件进行MD5值检测