VB.Net中的文件比较 [英] File comparison in VB.Net

查看:70
本文介绍了VB.Net中的文件比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道两个文件是否相同.首先,我比较了文件大小和创建时间戳,但这还不够可靠.我想出了下面的代码,似乎可以用,但是我希望有人能做得更好,更轻松或更快.

基本上,我正在做的是将文件内容流式传输到字节数组,并通过System.Security.Cryptography比较MD5哈希值.

在此之前,我先做一些简单的检查,因为没有理由通读文件,如果两个文件路径都相同,或者其中一个文件不存在.

 公共函数CompareFiles(ByVal file1FullPath作为字符串,ByVal file2FullPath作为字符串)作为布尔值如果不是File.Exists(file1FullPath)或Not File.Exists(file2FullPath)然后'一个或两个文件都不存在.传回False万一如果String.Compare(file1FullPath,file2FullPath,True)= 0,则'fileFullPath1和fileFullPath2指向同一个文件...返回真万一昏暗的MD5Crypto作为新的MD5CryptoServiceProvider()昏暗的textEncoding作为新的System.Text.ASCIIEncoding()Dim fileBytes1()作为字节,fileBytes2()作为字节Dim fileContents1,fileContents2作为字符串将StreamReader设为Dim streamReader =什么都没有昏暗的FileStream为FileStream = NothingDim isIdentical为Boolean = False尝试'将文件1读取到字节数组.fileStream =新的FileStream(file1FullPath,FileMode.Open)streamReader =新的StreamReader(fileStream)fileBytes1 = textEncoding.GetBytes(streamReader.ReadToEnd)fileContents1 = textEncoding.GetString(MD5Crypto.ComputeHash(fileBytes1))streamReader.Close()fileStream.Close()'将文件2读取到字节数组.fileStream =新的FileStream(file2FullPath,FileMode.Open)streamReader =新的StreamReader(fileStream)fileBytes2 = textEncoding.GetBytes(streamReader.ReadToEnd)fileContents2 = textEncoding.GetString(MD5Crypto.ComputeHash(fileBytes2))streamReader.Close()fileStream.Close()'比较字节数组并返回结果.isIdentical = fileContents1 = fileContents2异常捕获isIdentical = False最后如果不是streamReader则为空,则streamReader.Close()如果不是,则fileStream.Close()fileBytes1 =无fileBytes2 =什么都没有结束尝试返回值是相同的结束功能 

解决方案

我想说对文件进行散列是一种方法,这就是我过去所做的事情.

在使用Streams等进行清理时,请使用Using语句.这是一个例子.

 公共函数CompareFiles(ByVal file1FullPath作为字符串,ByVal file2FullPath作为字符串)作为布尔值如果不是File.Exists(file1FullPath)或Not File.Exists(file2FullPath)然后'一个或两个文件都不存在.传回False万一如果file1FullPath = file2FullPath然后'fileFullPath1和fileFullPath2指向同一个文件...返回真万一尝试Dim file1Hash as String = hashFile(file1FullPath)将Dim file2Hash转换为String = hashFile(file2FullPath)如果file1Hash = file2Hash然后返回真别的传回False万一异常捕获传回False结束尝试结束功能私有函数hashFile(ByVal filepath As String)As String将阅读器用作新System.IO.FileStream(filepath,IO.FileMode.Open,IO.FileAccess.Read)使用md5作为新的System.Security.Cryptography.MD5CryptoServiceProvider昏暗hash()为字节= md5.ComputeHash(阅读器)返回System.Text.Encoding.Unicode.GetString(哈希)最终使用最终使用结束功能 

I need to know if two files are identical. At first I compared file sizes and creation timestamps, but that's not reliable enough. I have come up with the following code, that seems to work, but I'm hoping that someone has a better, easier or faster way of doing it.

Basically what I am doing, is streaming the file contents to byte arrays, and comparing thier MD5 hashes via System.Security.Cryptography.

Before that I do some simple checks though, since there is no reason to read through the files, if both file paths are identical, or one of the files does not exist.

Public Function CompareFiles(ByVal file1FullPath As String, ByVal file2FullPath As String) As Boolean

    If Not File.Exists(file1FullPath) Or Not File.Exists(file2FullPath) Then
        'One or both of the files does not exist.
        Return False
    End If

    If String.Compare(file1FullPath, file2FullPath, True) = 0 Then
        ' fileFullPath1 and fileFullPath2 points to the same file...
        Return True
    End If

    Dim MD5Crypto As New MD5CryptoServiceProvider()
    Dim textEncoding As New System.Text.ASCIIEncoding()

    Dim fileBytes1() As Byte, fileBytes2() As Byte
    Dim fileContents1, fileContents2 As String
    Dim streamReader As StreamReader = Nothing
    Dim fileStream As FileStream = Nothing
    Dim isIdentical As Boolean = False

    Try

        ' Read file 1 to byte array.
        fileStream = New FileStream(file1FullPath, FileMode.Open)
        streamReader = New StreamReader(fileStream)
        fileBytes1 = textEncoding.GetBytes(streamReader.ReadToEnd)
        fileContents1 = textEncoding.GetString(MD5Crypto.ComputeHash(fileBytes1))
        streamReader.Close()
        fileStream.Close()

        ' Read file 2 to byte array.
        fileStream = New FileStream(file2FullPath, FileMode.Open)
        streamReader = New StreamReader(fileStream)
        fileBytes2 = textEncoding.GetBytes(streamReader.ReadToEnd)
        fileContents2 = textEncoding.GetString(MD5Crypto.ComputeHash(fileBytes2))
        streamReader.Close()
        fileStream.Close()

        ' Compare byte array and return result.
        isIdentical = fileContents1 = fileContents2

    Catch ex As Exception

        isIdentical = False

    Finally

        If Not streamReader Is Nothing Then streamReader.Close()
        If Not fileStream Is Nothing Then fileStream.Close()
        fileBytes1 = Nothing
        fileBytes2 = Nothing

    End Try

    Return isIdentical
End Function

解决方案

I would say hashing the file is the way to go, It's how I have done it in the past.

Use Using statements when working with Streams and such, as they clean themselves up. Here is an example.

Public Function CompareFiles(ByVal file1FullPath As String, ByVal file2FullPath As String) As Boolean

If Not File.Exists(file1FullPath) Or Not File.Exists(file2FullPath) Then
    'One or both of the files does not exist.
    Return False
End If

If file1FullPath = file2FullPath Then
    ' fileFullPath1 and fileFullPath2 points to the same file...
    Return True
End If

Try
    Dim file1Hash as String = hashFile(file1FullPath)
    Dim file2Hash as String = hashFile(file2FullPath)

    If file1Hash = file2Hash Then
        Return True
    Else
        Return False
    End If

Catch ex As Exception
    Return False
End Try
End Function

Private Function hashFile(ByVal filepath As String) As String
    Using reader As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
        Using md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
            Dim hash() As Byte = md5.ComputeHash(reader) 
            Return System.Text.Encoding.Unicode.GetString(hash) 
        End Using
    End Using
End Function

这篇关于VB.Net中的文件比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆