Objective
MD5 is a common way of encryption, relatively stable, but also a way to verify the file, this article introduces the use of C # to get file MD5 value of the method, directly use can be used to compare the file is the same. Here's a few words to say, look at the sample code
The sample code is as follows:
<summary>
///Get file MD5 value
///</summary>
///<param name= "fileName" > File absolute path </param >
///<returns>md5 value </returns> public
static string Getmd5hashfromfile (String fileName)
{
Try
{
FileStream file = new FileStream (FileName, FileMode.Open);
System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();
byte[] RetVal = md5.computehash (file);
File. Close ();
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < retval.length i++)
{
sb. Append (Retval[i]. ToString ("X2"));
}
Return SB. ToString ();
}
catch (Exception ex)
{
throw new Exception ("Getmd5hashfromfile () Fail,error:" + ex. message);
}
Summarize
The above is about C # get file MD5 value of all content, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.