3 Handy Icon Functions You Can UseBeware of These Three Software Development Blind Spots

How To Compare Files in VB.NET

October 3rd, 2007

Finding the differences between filesA common function you’ll need when you’re dealing with a lot of files is a way to compare them and report any differences. In this code example, we’ll look at a way of comparing files and capturing the differences between them as we go.

The Design

In this design we’re going to place our CompareFiles routine in a class called FileCompare. The routine itself, CompareFiles, will be a public shared method of the class since we won’t be saving any stateful information. We will also be adding a nested class called FileDifference that will hold the differences we find. Our function will accept the full file names of the two files to compare as string parameters and return a generic list of FileDifference objects.

The Code

First, we’ll look at the code for our function and then discuss a few points in it.

Public Shared Function CompareFiles(ByVal file1 As String, ByVal file2 As String) As List(Of FileDifference)
    Dim ListOfDifferences As New List(Of FileDifference)
    If file1 <> file2 Then
        If File.Exists(file1) AndAlso File.Exists(file2) Then
            Using Stream1 As New FileStream(file1, FileMode.Open), Stream2 As New FileStream(file2, FileMode.Open)
                If Stream1.Length <> Stream2.Length Then
                    ListOfDifferences = Nothing
                Else
                    Dim File1Content As Integer
                    Dim File2Content As Integer
                    Do
                        File1Content = Stream1.ReadByte()
                        File2Content = Stream2.ReadByte()
                        If File1Content <> File2Content Then
                            ListOfDifferences.Add(New FileDifference(File1Content, File2Content, Stream1.Position))
                        End If
                    Loop While (File1Content <> -1)
                End If
            End Using
        Else
            ListOfDifferences = Nothing
        End If
    End If
    Return ListOfDifferences
End Function

If the same file name is passed in for both parameters we’ll return an empty list. If one of the targeted files doesn’t exists or if they are of different lengths, we’ll return Nothing. Otherwise we’ll return the actual differences, if there are any.

Notice that the Using block can work with multiple objects, in this case our two FileStreams. Using will also take care of closing and disposing of the streams. Some people like to throw in a call to the Close method just for good measure. You can add this in just before the End Using if you feel more comfortable having it there.

When the bytes in the file don’t match, we create a new FileDifference object and add it to our list.

Now, let’s take a look at our FileDifference class

Public Class FileDifference
    Private _file1Content As Integer
    Private _file2Content As Integer
    Private _filePosition As Long

    Public Sub New(ByVal content1 As Integer, ByVal content2 As Integer, ByVal position As Long)
        _file1Content = content1
        _file2Content = content2
        _filePosition = position
    End Sub

    Public ReadOnly Property File1Content() As Integer
        Get
            Return _file1Content
        End Get
    End Property

    Public ReadOnly Property File2Content() As Integer
        Get
            Return _file2Content
        End Get
    End Property

    Public ReadOnly Property FilePosition() As Long
        Get
            Return _filePosition
        End Get
    End Property

End Class

There’s nothing that special about this class. It’s just a handy way for us to pass back related values.

When you call this function from your code it would look something like this:

'
'
Dim Differences As List(Of FileCompare.FileDifference)
Differences = FileCompare.CompareFiles(txtFile1.Text, txtFile2.Text)
'
'

Extending It

Of course, this class is just the basics and you will probably want to find ways to extend it for your purposes. For example, you may want to override the ToString function of the FileDifference class to return something you can plug directly into a listbox, such as this:

Public Overrides Function ToString() As String
    Return String.Concat("File 1: 0x", _file1Content.ToString("x"), _
                         " File 2: 0x", _file2Content.ToString("x"), _
                         " at position ", _filePosition)
End Function

You might also want to create a version that deals with text files and compares their contents on a line by line or word by word basis. You could even expand it into a more complex compare and analyze solution if you wanted.

That’s it for this code example. I hope you’ve found it useful. You can click here to download the class file if you wish.. Let me know if you have any questions or observations about this code example by leaving me a comment.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: Code Examples


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (2 votes, average: 4.5 out of 5)
Loading ... Loading ...

1 Comment Add your own

  • 1. kroslki  |  January 27th, 2010 at 6:51 pm

    This example is somewhat useful, when comparing 2 files of the exact same length. That is a shortcoming, as it is much more common to compare 2 files of different sizes.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives