08/02/15 11:52:15
>>35-37の応用で、ファイルの重複を検出するコードをLINQでコンパクトに書いてみます。
次のメソッドが存在するという前提です。
static string GetMD5String(string filename)
{
using (System.Security.Cryptography.MD5CryptoServiceProvider md5
= new System.Security.Cryptography.MD5CryptoServiceProvider())
{
byte[] data = System.IO.File.ReadAllBytes(filename);
byte[] hash = md5.ComputeHash(data);
StringBuilder buf = new StringBuilder();
foreach (byte b in hash)
{
buf.AppendFormat("{0:x2}", b);
}
return buf.ToString();
}
}