Compares the content of 2 files : File Util « File Stream « C# / C Sharp






Compares the content of 2 files

   

#region License and Copyright
/* -------------------------------------------------------------------------
 * Dotnet Commons IO
 *
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the 
 * 
 * Free Software Foundation, Inc., 
 * 59 Temple Place, 
 * Suite 330, 
 * Boston, 
 * MA 02111-1307 
 * USA 
 * 
 * -------------------------------------------------------------------------
 */
#endregion

using System;
using System.Collections;
using System.Globalization;
using System.IO;

namespace Dotnet.Commons.IO
{
    class MainClass{
        /// ---------------------------------------------------------------
        /// <summary>
        /// This method compares the content of 2 files. A return value of true
        /// indicates that the contents of the files
        /// are the same. A return value of false indicates that the 
        /// files are not the same.
    /// </summary>
    /// <param name="firstFile">first file to be compared against the second</param>
    /// <param name="secondFile">second file to be compared against the first</param>
    /// <returns>true if they are the same, false otherwise</returns>
        /// <remarks>
        /// This method has been modified to increase the speed in which this
        /// file comparing operation performs.
        /// 
        /// The MSDN knowledge base:
        /// How to create a File-Compare function in Visual C#
        /// http://support.microsoft.com/default.aspx?scid=kb;en-us;320348
        /// was used to assist in speeding up this operation.
        /// </remarks>
        /// ---------------------------------------------------------------
    public static bool Compare(FileInfo firstFile, FileInfo secondFile)
    {
      if (!firstFile.Exists) 
      {
        string message = "File '" + firstFile.FullName + "' does not exist";
        throw new FileNotFoundException(message);
      }

      if (!secondFile.Exists) 
      {
        string message = "File '" + secondFile.FullName + "' does not exist";
        throw new FileNotFoundException(message);
      }


      // Check Each byte
      FileStream fs1 = new FileStream(firstFile.FullName, FileMode.Open, FileAccess.Read);
            FileStream fs2 = new FileStream(secondFile.FullName, FileMode.Open, FileAccess.Read);
                     
            // Check the file sizes. If they are not the same, the files 
            // are not the same.
            if (fs1.Length != fs2.Length)
            {
                // Close the file
                fs1.Close();
                fs2.Close();

                // Return false to indicate files are different
                return false;
            }

            int file1byte;
            int file2byte;

            // Read and compare a byte from each file until either a
            // non-matching set of bytes is found or until the end of
            // file1 is reached.
            do
            {
                // Read one byte from each file.
                file1byte = fs1.ReadByte();
                file2byte = fs2.ReadByte();
            }
            while ((file1byte == file2byte) && (file1byte != -1));

            // Close the files.
            fs1.Close();
            fs2.Close();

            // Return the success of the comparison. "file1byte" is 
            // equal to "file2byte" at this point only if the files are 
            // the same.
            return ((file1byte - file2byte) == 0);

    }

        


        /// <summary>
        /// Compare the contents two files.
        /// </summary>
        /// <param name="firstFile">first file to be compared against the second</param>
        /// <param name="secondFile">second file to be compared against the first</param>
        /// <returns>true if they are the same, false otherwise</returns>
        public static bool Compare(string firstFile, string secondFile)
        {
            FileInfo fiFirstFile = new FileInfo(firstFile);
            FileInfo fiSecondFile = new FileInfo(secondFile);

            return Compare(fiFirstFile, fiSecondFile);
        }
   }
}

   
    
    
  








Related examples in the same category

1.Returns a human-readable version of the file size (original is in bytes).
2.Copy file from source to destination
3.Clean a directory without deleting it
4.Implements the same behaviour as the "touch" utility on Unix.
5.Gets information about the files in a directory and puts it in an array of strings.
6.Checks if the giving File exists, and returns its length
7.Read the content of the text file.
8.Returns an array of abstract pathnames representing the files and directories of the specified path.
9.Get all the files that matches a wildcard pattern, eg. (*.tmp)
10.Tests if the specified file is newer than the reference file.
11.Returns true if the file specified by the pathname is a hidden file.
12.Checks if a file have write permissions
13.Remove a file or similar files if wildcard is included.
14.Sets the read-only property of the file to true.
15.Delete a file if exist
16.Get File SystemInfo
17.Saves a file
18.Renames a file
19.Determines if a directory exists
20.Deletes files newer than the specified date
21.Compares 2 files and determines if they are the same or not
22.Saves a file to an FTP server
23.Make file writable and copy
24.Create Temp File
25.Write File methods
26.Append File methods
27.Copy a file to a different filename, with cleaning null characters.
28.Read the given filename and yield return a string
29.Get Files / Get Folders methods
30.return true if the filename has the given attribute set
31.Copy from one file to another file
32.Get a 32x32 icon for a given file
33.Create Thumbnail
34.removes invalid charactes from filenames, like the slash and backslash
35.Create Thumbnail Image
36.Removes invalid file name characters from the specified string.
37.Append a suffix (such as a date) to the name of the file.
38.Copy one folder to another folder
39.Checks if a given file exists
40.File size format
41.Format Byte in B, KB, MB, GB