This article is from Microsoft Support. The orignal article is on http://support.microsoft.com/kb/320348.
This step-by-step article demonstrates how to compare two files to see if their contents are the same. This comparison looks at the contents of the two files, not at the file names, locations, dates, times, or other attributes.
This functionality is similar to the MS-DOS-based Fc.exe utility that is included with various versions of Microsoft Windows and Microsoft MS-DOS, and with some development tools.
The sample code that is described in this article performs a byte-by-byte comparison until it finds a mismatch or it reaches the end of the file. The code also performs two simple checks to increase the efficiency of the comparison:
- If both file references point to the same file, the two files must be equal.
- If the size of the two files is not the same, the two files are not the same.
To Create the Sample
- Create a new Visual C# Windows Application project. By default, Form1 is created.
- Add two textbox controls to the form.
- Add a command button to the form.
- On the View menu, click Code .
- Add the following USING statement to the Form1 class:
- Add the following method to the Form1 class:
- Paste the following code in the Click event of the command button:
- Save and then run the sample.
- Supply the full paths to the two files in the textboxes, and then click the command button.
For more information, visit the following Microsoft Web sites:
( http://msdn2.microsoft.com/en-us/library/system.io(vs.71).aspx )
( http://msdn2.microsoft.com/en-us/library/Aa903246(VS.71).aspx )