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.
精彩评论