VB.Net中的文件比较

编程入门 行业动态 更新时间:2024-10-23 12:32:14
本文介绍了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中的文件比较

本文发布于:2023-11-04 05:29:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557077.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   VB   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!