C# 檔案MD5計算


 

資料來源: http://www.dotblogs.com.tw/junegoat/archive/2012/08/21/c-sharp-md5-sha1-sha256-valid-file-correct.aspx




Code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//http://www.dotblogs.com.tw/junegoat/archive/2012/08/21/c-sharp-md5-sha1-sha256-valid-file-correct.aspx

namespace Console_CS_File_MD5

{

    class Program

    {

        static void Pause()

        {

            Console.Write("Press any key to continue . . . ");

            Console.ReadKey(true);

        }

        static void Main(string[] args)

        {

            var tragetFile = new System.IO.FileStream(@"C:\Users\RD\Desktop\Release\Ionic.Zip.dll", System.IO.FileMode.Open);

            var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] hashbytes = md5.ComputeHash(tragetFile);

            tragetFile.Close();

 

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            for (int i = 0; i < hashbytes.Length; i++)

            {

                sb.Append(hashbytes[i].ToString("x2"));

            }

            Console.WriteLine(sb.ToString());

            Pause();

        }

    }

} 
相关文章