#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);
}
}
}
|