How To Compare Two Files with VB.NET and VB2005

This article and source code demonstrate how to use Visual Basic 2005 to compare two files to determine if they are equal. This comparison looks at file paths, file lengths, and file contents. NOTE: While source code for VB.NET is not provided, the CompareFiles function included in this article will work with VB.NET too. Be sure to Import System.IO.

Application Screen Shot

The source code includes a Windows Forms application.

Source Code Excerpt

After a user makes two file selections and clicks the ‘Compare Files’ button, the CompareFiles function in the source code performs the file comparison.

First CompareFiles compares the path to file one to the path to file two.  If they are the same, file one and file two are the same.

Next, CompareFiles compares the length of file one to the length of file two.  If they are different the files are not equal.

Finally, if file paths are different and the length of file one and file two are the same, CompareFiles compares the contents of file one to file two, byte-by-byte. If the bytes in file one are equal to the bytes in file two, the files are equal. The System.IO.FileStream class is used to read one byte at a time from each file. A FileStream object exposes a stream around a file, supporting both synchronous and asynchronous read and write operations.


Private Function CompareFiles(ByVal filePathOne As String, ByVal filePathTwo As String) As Boolean


    Dim fileOneByte As Integer

    Dim fileTwoByte As Integer


    Dim fileOneStream As FileStream

    Dim fileTwoStream As FileStream


    ‘ If user has selected the same file as file one and file two….

    If (filePathOne = filePathTwo) Then

        ‘ Files are the same.

        Me.ResultsRichTextBox.Text = “Files are the same; file one is file two.”

        Return True

    End If


    ‘ Open a FileStream for each file.

    fileOneStream = New FileStream(filePathOne, FileMode.Open)

    fileTwoStream = New FileStream(filePathTwo, FileMode.Open)


    ‘ If the files are not the same length…

    If (fileOneStream.Length <> fileTwoStream.Length) Then



        ‘ File’s are not equal.

        Me.ResultsRichTextBox.Text = “Files are not the same length; they are not equal.”

        Return False

    End If


    Dim areFilesEqual As Boolean = True


    ‘ Loop through bytes in the files until

    ‘  a byte in file one <> a byte in file two

    ‘ OR

    ‘  end of the file one is reached.


        ‘ Read one byte from each file.

        fileOneByte = fileOneStream.ReadByte()

        fileTwoByte = fileTwoStream.ReadByte()

        If fileOneByte <> fileTwoByte Then

            ‘ Files are not equal; byte in file one <> byte in file two.

            Me.ResultsRichTextBox.Text = “Files are not equal; contents are different.”

            areFilesEqual = False

            Exit Do

        End If

    Loop While (fileOneByte <> -1)


    ‘ Close the FileStreams.




    Return areFilesEqual


 End Function


~ bởi duriangroup trên Tháng Sáu 1, 2007.

%d bloggers like this: