{"id":404,"date":"2019-01-15T16:30:51","date_gmt":"2019-01-15T21:30:51","guid":{"rendered":"http:\/\/techref.camellarry.com\/?p=404"},"modified":"2019-01-15T16:30:51","modified_gmt":"2019-01-15T21:30:51","slug":"compare-files","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=404","title":{"rendered":"Compare Files"},"content":{"rendered":"<blockquote><p><strong>This example illustrates how to compare two files.<\/strong><\/p><\/blockquote>\n<pre>Imports System.IO\r\nImports System.Security.Cryptography\r\n\r\n<span style=\"color: #ff0000;\">Public Class Form1<\/span>\r\n\r\n<span style=\"color: #0000ff;\">Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load<\/span>\r\n\r\nIf CompareFiles(\"C:\\Temp\\2filecopy.bat\", \"C:\\Temp\\4filecopy.bat\") = True Then\r\n  MsgBox(\"The same\")\r\nElse\r\n  MsgBox(\"Different\")\r\nEnd If\r\n\r\n  End\r\n<span style=\"color: #0000ff;\">End Sub<\/span>\r\n\r\n<span style=\"color: #0000ff;\">Public Function CompareFiles(ByVal FileFullPath1 As String, ByVal FileFullPath2 As String) As Boolean<\/span>\r\n'returns true if two files passed to is are identical, false otherwise\r\n'does byte comparison; works for both text and binary files\r\n\r\n'Throws exception on errors; you can change to just return \r\n'false if you prefer\r\n\r\nDim objMD5 As New MD5CryptoServiceProvider()\r\nDim objEncoding As New System.Text.ASCIIEncoding()\r\nDim aFile1() As Byte, aFile2() As Byte\r\nDim strContents1, strContents2 As String\r\nDim objReader As StreamReader\r\nDim objFS As FileStream\r\nDim bAns As Boolean\r\n\r\n<span style=\"color: #999999;\">'If Not File.Exists(FileFullPath1) Then _<\/span>\r\n<span style=\"color: #999999;\">' Throw New Exception(FileFullPath1 &amp; \" doesn't exist\")<\/span>\r\n<span style=\"color: #999999;\">'If Not File.Exists(FileFullPath2) Then _<\/span>\r\n<span style=\"color: #999999;\">' Throw New Exception(FileFullPath2 &amp; \" doesn't exist\")<\/span>\r\n\r\nIf Not File.Exists(FileFullPath1) Or Not File.Exists(FileFullPath2) Then\r\n  Return False\r\n  Exit Function\r\nEnd If\r\n\r\nTry\r\n  objFS = New FileStream(FileFullPath1, FileMode.Open)\r\n  objReader = New StreamReader(objFS)\r\n  aFile1 = objEncoding.GetBytes(objReader.ReadToEnd)\r\n  strContents1 = _\r\n  objEncoding.GetString(objMD5.ComputeHash(aFile1))\r\n  objReader.Close()\r\n  objFS.Close()\r\n\r\n  objFS = New FileStream(FileFullPath2, FileMode.Open)\r\n  objReader = New StreamReader(objFS)\r\n  aFile2 = objEncoding.GetBytes(objReader.ReadToEnd)\r\n  strContents2 = _\r\n  objEncoding.GetString(objMD5.ComputeHash(aFile2))\r\n\r\n  bAns = strContents1 = strContents2\r\n  objReader.Close()\r\n  objFS.Close()\r\n  aFile1 = Nothing\r\n  aFile2 = Nothing\r\n\r\nCatch ex As Exception\r\n  Throw ex\r\nEnd Try\r\n\r\nReturn bAns\r\n<span style=\"color: #0000ff;\">End Function<\/span>\r\n<span style=\"color: #ff0000;\">End Class<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example illustrates how to compare two files. Imports System.IO Imports System.Security.Cryptography Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load If CompareFiles(&#8220;C:\\Temp\\2filecopy.bat&#8221;, &#8220;C:\\Temp\\4filecopy.bat&#8221;) = True Then MsgBox(&#8220;The same&#8221;) Else MsgBox(&#8220;Different&#8221;) End If End End Sub Public Function CompareFiles(ByVal FileFullPath1 As String, ByVal FileFullPath2 As String) As Boolean &#8216;returns true if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[29,58,76,77,78,80],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-vb-net","tag-example","tag-programming","tag-vb","tag-vb-net","tag-visualbasic","tag-windows"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-6w","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=404"}],"version-history":[{"count":0,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/404\/revisions"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}