Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code that I originally found in the VB Cookbook that checks 2 files for equality. The solution generates a checksum on each file and then compares the checksums. Everything has been working fine in my original app so I recently decided to use the same logic in another app. The reason I want to use this code is that I want to validate that a text file that I just copied from a network server matches the file on the network. While testing my new app, I noticed that I am getting random false positives. (App says the files aren't equal, but text comparison software says they are equal) If I close and restart the app and immediately copy and compare the files again, then it says they are equal. If we don't close the app and just try it again, it still says they are unequal. I even tested the app on another similar machine and I am having random issues on that machine as well.

I am trying to figure out why this is happening? If the file is being used by another process, would this cause an issue? Could it be hardware related? These machines are still running Win 2000 and have limited RAM. Just trying to get an idea on what could be wrong so I can try something different.

What I have tried:

VB
Public Function CopyAndCheckIfFilesAreIdentical(ByVal SourceFileName As String, ByVal DestFileName As String, ByVal OverwriteFile As Boolean) As Integer


        'This function copies a file and then compares the copied file using a hashing algorithm
        'Result = 1 = Files are identical
        'Result = 0 = Files are different
        'Result = -1 = File(s) don't exist

        Dim Result As Integer = 0
        Dim TheFilesAreIdentical As Boolean


        'Copy the file first
        System.IO.File.Copy(SourceFileName, DestFileName, OverwriteFile)

        'Make sure the files exist
        If System.IO.File.Exists(SourceFileName) = True And System.IO.File.Exists(DestFileName) = True Then
            TheFilesAreIdentical = AreFilesIdentical(SourceFileName, DestFileName)
            If TheFilesAreIdentical = True Then
                Result = 1
            Else
                Result = 0
                MsgBox("Source File:  " & SourceFileName & vbNewLine & _
                        "Is NOT identical to" & vbNewLine & _
                        "Dest File:  " & DestFileName, MsgBoxStyle.Critical, "Files not Identical")

            End If

        Else
            Result = -1
            If System.IO.File.Exists(SourceFileName) = False Then
                MsgBox("Source File:  " & SourceFileName & vbNewLine & _
                        "Does not exist" & vbNewLine & _
                        "During Function:  CopyandCheckIfFilesAreIdentical")
            Else
                MsgBox("Dest File:  " & DestFileName & vbNewLine & _
                        "Does not exist" & vbNewLine & _
                        "During Function:  CopyandCheckIfFilesAreIdentical")

            End If

        End If

        Return Result


    End Function


    Public Function AreFilesIdentical(ByVal File1 As String, ByVal File2 As String) As Boolean


        'Return True if the files are identical

        Dim CheckSum1 As Byte()
        Dim CheckSum2 As Byte()
        Dim CheckSum1asString As String
        Dim CheckSum2asString As String
        Dim Counter As Integer


        On Error GoTo ErrorHandler

        'Calculate the CheckSums
        CheckSum1 = GenerateFileCheckSum(File1)
        CheckSum2 = GenerateFileCheckSum(File2)

        CheckSum1asString = BitConverter.ToString(CheckSum1)
        CheckSum2asString = BitConverter.ToString(CheckSum2)

        'See if the results are equal
        For Counter = 0 To UBound(CheckSum1)
            If CheckSum1(Counter) <> CheckSum2(Counter) Then
                Return False
            End If
        Next

        'The CheckSums are True
        Return True


ErrorHandler:

        Return False

    End Function



    Public Function GenerateFileCheckSum(ByVal Filepath As String) As Byte()

        'This function generates a checksum of the file

        Dim HashingFunction As HMACSHA1
        Dim HashingBase() As Byte
        Dim HashValue() As Byte
        Dim InStream As IO.Stream


        'Make sure the file exists
        If My.Computer.FileSystem.FileExists(Filepath) = False Then
            Throw New IO.FileNotFoundException
            Return Nothing
        End If

        'Prepare the hashing key.  You have to use the same hashing key everytime to get the same results
        HashingBase = (New UnicodeEncoding).GetBytes("Cookbook")

        'Create the hashing component using the Managed SHA-1 function
        HashingFunction = New HMACSHA1(HashingBase, True)

        'Open the file as a stream
        InStream = New IO.FileStream(Filepath, IO.FileMode.Open, IO.FileAccess.Read)

        'Calculate the checksum value
        HashValue = HashingFunction.ComputeHash(InStream)

        'Finished with the file
        InStream.Close()

        'Return the checksum as a byte array
        Return HashValue


    End Function
Posted
Updated 30-Aug-17 10:21am
v2

1 solution

Quote:
If the file is being used by another process, would this cause an issue?
Well ... this code:
VB
        On Error GoTo ErrorHandler
...
ErrorHandler:
        Return False
Would mean that if the file was in use, it would return false - so anything which means it can't complete the task would get you a "random" "no match" signal.

But please, forget "On Error ..." completely, and use Try ... Catch blocks. They will give you a lot better info as to what happened to cause the problem as part of the Exception object, and that would mean that tracking down exactly why it's a problem should be a load easier.

Since it's intermittent, I'd probably start by checking the same file for equality: if that ever fails, it's not the file itself that is causing the problem...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900