How to create a file-compare function in Visual Basic .NET or in Visual Basic 2005

For a Microsoft Visual C# .NET version of this article, see 320348 .

IN THIS TASK

Summary

This step-by-step article demonstrates how to compare two files to see if their contents are the same. This comparison looks at the contents of the two files, not at the file names, locations, dates, times, or other attributes.

This functionality is similar to the MS-DOS-based Fc.exe utility that is included with various versions of Microsoft Windows and Microsoft MS-DOS, and with some development tools.

The sample code that is described in this article performs a byte-by-byte comparison until it finds a mismatch or it reaches the end of the file. The code also performs two simple checks to increase the efficiency of the comparison:

  • If both file references point to the same file, the two files must be equal.
  • If the size of the two files is not the same, the two files are not the same.

To Create the Sample

  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows Application project. By default, Form1 is created.

    Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

    For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

  2. Add two textbox controls to the form.
  3. Add a command button to the form.
  4. On the View menu, click Code.
  5. Add the following IMPORTS statement to the Form1 class:
    Imports System.IO
  6. Add the following method to the Form1 class:
    ' This method accepts two strings that represent two files to 
    ' compare. A return value of 0 indicates that the contents of the files
    ' are the same. A return value of any other value indicates that the
    ' files are not the same.
    Private Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean
    Dim file1byte As Integer
    Dim file2byte As Integer
    Dim fs1 As FileStream
    Dim fs2 As FileStream

    ' Determine if the same file was referenced two times.
    If (file1 = file2) Then
    ' Return 0 to indicate that the files are the same.
    Return True
    End If

    ' Open the two files.
    fs1 = New FileStream(file1, FileMode.Open)
    fs2 = New FileStream(file2, FileMode.Open)

    ' Check the file sizes. If they are not the same, the files
    ' are not equal.
    If (fs1.Length <> fs2.Length) Then
    ' Close the file
    fs1.Close()
    fs2.Close()

    ' Return a non-zero value to indicate that the files are different.
    Return False
    End If

    ' 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()
    Loop While ((file1byte = file2byte) And (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)
    End Function
  7. Paste the following code in the Click event handler for the command button:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Compare the two files that are referenced in the textbox controls.
    If (FileCompare(Me.TextBox1.Text, Me.TextBox2.Text)) Then
    MessageBox.Show("Files are equal.")
    Else
    MessageBox.Show("Files are not equal.")
    End If
    End Sub
  8. Save and the run the sample code.
  9. Supply the full paths to two files in the text boxes, and then click the command button.

References

For additional information, visit the following Microsoft Web sites:

Propriedades

ID do Artigo: 320346 - Última Revisão: 11 de mai de 2007 - Revisão: 1

Comentários