C # File Comparison code

Source: Internet
Author: User

I think we often want to compare a file inside whether there are changes, such as a DLL library is a new method or modify the implementation of the method, can not be compared by the visual tools to compare, the use of this gadget to compare, the following is the code to compare files.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespacefilescompare{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please type the first file."); stringFirstfilepath =Console.ReadLine (); Console.WriteLine ("Please type the second file."); stringSecondfilepath =Console.ReadLine (); BOOLIsEqual =comparefiles (Firstfilepath, Secondfilepath); Console.WriteLine (IsEqual?"Equal":"Not equal");        Console.read (); }         Public Static BOOLComparefiles (stringTargetFile,stringupdatedfile) {FileInfo Targetfileinfo=NewFileInfo (targetfile); FileInfo Updatedfileinfo=NewFileInfo (Updatedfile); if(Targetfileinfo.length! =updatedfileinfo.length) {return false; }            using(FileStream Targetstream =File.openread (targetfile)) {                using(FileStream Updatedstream =File.openread (Updatedfile)) {                    if(Targetstream.length! =updatedstream.length) {return false; }                    //Using a larger buffer than the default buffer of 4 * 1024x768 used by Filestream.readbyte improves performance. //The buffer size is based on user feedback. Based on performance results, a better buffer size could be determined.                    byte[] Targetbuffer =New byte[ -*1024x768]; byte[] Updatedbuffer =New byte[ -*1024x768]; inttargetreadlength; intupdatedreadlength;  Do{targetreadlength= Targetstream.read (Targetbuffer,0, targetbuffer.length); Updatedreadlength= Updatedstream.read (Updatedbuffer,0, updatedbuffer.length); if(Targetreadlength! =updatedreadlength) {                            return false; }                         for(inti =0; i < targetreadlength; ++i) {if(Targetbuffer[i]! =Updatedbuffer[i]) {                                return false; }                        }                    }  while(0<targetreadlength); }            }            return true; }    }}

C # File Comparison code

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.