C # Implementation example of fetching file MD5 value _c# Tutorial

Source: Internet
Author: User
Tags md5

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.