This example illustrates how to compare two files.
Imports System.IO
Imports System.Security.Cryptography
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If CompareFiles("C:\Temp\2filecopy.bat", "C:\Temp\4filecopy.bat") = True Then
MsgBox("The same")
Else
MsgBox("Different")
End If
End
End Sub
Public Function CompareFiles(ByVal FileFullPath1 As String, ByVal FileFullPath2 As String) As Boolean
'returns true if two files passed to is are identical, false otherwise
'does byte comparison; works for both text and binary files
'Throws exception on errors; you can change to just return
'false if you prefer
Dim objMD5 As New MD5CryptoServiceProvider()
Dim objEncoding As New System.Text.ASCIIEncoding()
Dim aFile1() As Byte, aFile2() As Byte
Dim strContents1, strContents2 As String
Dim objReader As StreamReader
Dim objFS As FileStream
Dim bAns As Boolean
'If Not File.Exists(FileFullPath1) Then _
' Throw New Exception(FileFullPath1 & " doesn't exist")
'If Not File.Exists(FileFullPath2) Then _
' Throw New Exception(FileFullPath2 & " doesn't exist")
If Not File.Exists(FileFullPath1) Or Not File.Exists(FileFullPath2) Then
Return False
Exit Function
End If
Try
objFS = New FileStream(FileFullPath1, FileMode.Open)
objReader = New StreamReader(objFS)
aFile1 = objEncoding.GetBytes(objReader.ReadToEnd)
strContents1 = _
objEncoding.GetString(objMD5.ComputeHash(aFile1))
objReader.Close()
objFS.Close()
objFS = New FileStream(FileFullPath2, FileMode.Open)
objReader = New StreamReader(objFS)
aFile2 = objEncoding.GetBytes(objReader.ReadToEnd)
strContents2 = _
objEncoding.GetString(objMD5.ComputeHash(aFile2))
bAns = strContents1 = strContents2
objReader.Close()
objFS.Close()
aFile1 = Nothing
aFile2 = Nothing
Catch ex As Exception
Throw ex
End Try
Return bAns
End Function
End Class