Comparador de archivos Curso de Programación Visual Basic

Lección:

Administración de Archivos


Ejercicio:

Comparador de archivos 5


Objetivo:

Cree un programa de Visual Basic para saber si dos archivos (de cualquier tipo) son idénticos (tienen el mismo contenido).


Código:

Imports System
Imports System.IO
Namespace FileComparer
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim equal As Boolean = True
            Dim myFile1 As FileStream
            Dim dataFile1 As Byte()
            Dim myFile2 As FileStream
            Dim dataFile2 As Byte()
            Console.Write("Enter the name of file1: ")
            Dim fileName1 As String = Console.ReadLine()
            Console.Write("Enter the name of file2: ")
            Dim fileName2 As String = Console.ReadLine()

            If (Not File.Exists(fileName1)) OrElse (Not File.Exists(fileName2)) Then
                Console.WriteLine("The file 1 or file 2 not exists!!!")
                Return
            End If

            Try
                myFile1 = File.OpenRead(fileName1)
                dataFile1 = New Byte(myFile1.Length - 1) {}
                myFile1.Read(dataFile1, 0, CInt(myFile1.Length))
                myFile1.Close()
                myFile2 = File.OpenRead(fileName2)
                dataFile2 = New Byte(myFile2.Length - 1) {}
                myFile2.Read(dataFile2, 0, CInt(myFile2.Length))
                myFile2.Close()

                If myFile1.Length = myFile2.Length Then

                    For i As Integer = 0 To dataFile1.Length - 1

                        If dataFile1(i) <> dataFile2(i) Then
                            equal = False
                        Else
                            equal = False
                        End If
                    Next
                End If

                If equal Then
                    Console.WriteLine("The {0} is equal {1}", fileName1, fileName2)
                Else
                    Console.WriteLine("The {0} not is equal {1}", fileName1, fileName2)
                End If

                Console.ReadLine()
            Catch e As Exception
                Console.WriteLine("Error: {0}!!!", e.Message)
            End Try
        End Sub
    End Class
End Namespace