Thursday, August 6, 2009

Compare the content of two files

Use the following snippet:

using System.IO;

. . .


private static bool FileCompare(string filePath1, string filePath2)
{
    int file1byte;
    int file2byte;
    FileStream fileStream1;
    FileStream fileStream2;
    if (filePath1 == filePath2)
    {
        return true;
    }
    FileInfo fileInfo = new FileInfo(filePath1);
    fileInfo.IsReadOnly = false;
    fileInfo = new FileInfo(filePath2);
    fileInfo.IsReadOnly = false;
    fileStream1 = new FileStream(filePath1, FileMode.Open);
    fileStream2 = new FileStream(filePath2, FileMode.Open);
    if (fileStream1.Length != fileStream2.Length)
    {
        fileStream1.Close();
        fileStream2.Close();
        return false;
    }
    do
   
{
        file1byte = fileStream1.ReadByte();
        file2byte = fileStream2.ReadByte();
    }
    while ((file1byte == file2byte) && (file1byte != -1));
    fileStream1.Close();
    fileStream2.Close();
    return ((file1byte - file2byte) == 0);
}

 

No comments:

Post a Comment