標籤:des style blog color io os 使用 ar for
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值檢測