tags:

views:

71

answers:

2

How can the MD5 Hash of a file be calculated and displayed in a label?

+4  A: 

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();
    }
}
Darin Dimitrov
and how would you do all this all self contained (i.e no read from other text files) O.o?
NightsEVil
@NightsEVil, I am not sure I am following your thought...
Darin Dimitrov
like wouldnt there be a way for the application to generate the md5 hash and display it in a label without it having to read it from a text file?
NightsEVil
+1  A: 

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.

Ralph
Of course, you will need to encode it, in hex/base64/etc.
BlueRaja - Danny Pflughoeft
and how would you do all this all self contained (i.e no read from other text files) O.o?
NightsEVil