开发者

calculate and display file MD5 Hash in a label

How can the MD5 Hash of a file be ca开发者_运维问答lculated and displayed in a label?


Yes it is possible:

label1.Text = GetMD5HashFromFile("somefile.txt");

where the GetMD5HashFromFile function could look like this:

public static string GetMD5HashFromFile(string filename)
{
    using (var md5 = new MD5CryptoServiceProvider())
    {
        var buffer = md5.ComputeHash(File.ReadAllBytes(filename));
        var sb = new StringBuilder();
        for (int i = 0; i < buffer.Length; i++)
        {
            sb.Append(buffer[i].ToString("x2"));
        }
        return sb.ToString();
    }
}


Yes, it's possible. When you calculate the MD5 Hash of a file you just need to take the result and place it in as the text of Label control. No problem there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜