Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to check if two files are equal in ASP.NET

Author: Richard Wand
Download Source Code : 1331_CheckTwoFilesEqual.zip

This article will explain how we can compare two files if they are equal in ASP.NET.

 

File duplication can cause problem for us when we have to deal with lots of files. Most of the times we only check file names, locations or creation dates of the files to compare files. Along with these attributes we should also check contents of the file.

We can compare two files using byte by byte comparison. In this article, we will compare contents of two files using Hash Algorithm to check if they are equal or not. ASP.NET provides a class “HashAlgorithm” for these purposes. If there is a single bit change in both files then Hash Algorithm will give us different results hence both files will not be equal.

 

 

  1. Create new website in Visual Studio 2010
  2. Add a Button and a Label in aspx page of a web form

    <asp:Button ID="Button1" runat="server" Text="Compare Files"
                onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label>

     
  3. Create two text files to compare or use already created files. I have created two text files with same content but with different names.
     
  4. We have to use following namespaces
     
    C#

    using System.IO;
    using System.Security.Cryptography;
     
    VB.NET

    Imports System.IO
    Imports System.Security.Cryptography
     
  5. Now Add a Button click event and write code below in it.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        string file1 = "C:\\MyFiles\\TextFile1.txt";
        string file2 = "C:\\MyFiles\\TextFile2.txt";
     
        if (File.Exists(file1) && File.Exists(file2))
        {
     
            HashAlgorithm hashAlgo = HashAlgorithm.Create();
     
            FileStream stream1 = new FileStream(file1, FileMode.Open);
            FileStream stream2 = new FileStream(file2, FileMode.Open);
     
            byte[] hash1;
            byte[] hash2;
     
            hash1 = hashAlgo.ComputeHash(stream1);
            hash2 = hashAlgo.ComputeHash(stream2);
     
            stream1.Close();
            stream2.Close();
     
            if (Convert.ToBase64String(hash1) == Convert.ToBase64String(hash2))
            {
                Label1.Text = "Files are equal";
            }
            else
            {
                Label1.Text = "Files are not equal";
            }
        }
        else
        {
            Label1.Text = "One of the file doesn’t exists";
        }
     
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim file1 As String = "C:\MyFiles\TextFile1.txt"
        Dim file2 As String = "C:\MyFiles\TextFile2.txt"
     
        If File.Exists(file1) AndAlso File.Exists(file2) Then
     
            Dim hashAlgo As HashAlgorithm = HashAlgorithm.Create()
     
            Dim stream1 As New FileStream(file1, FileMode.Open)
            Dim stream2 As New FileStream(file2, FileMode.Open)
     
            Dim hash1 As Byte()
            Dim hash2 As Byte()
     
            hash1 = hashAlgo.ComputeHash(stream1)
            hash2 = hashAlgo.ComputeHash(stream2)
     
            stream1.Close()
            stream2.Close()
     
            If Convert.ToBase64String(hash1) = Convert.ToBase64String(hash2) Then
                Label1.Text = "Files are equal"
            Else
                Label1.Text = "Files are not equal"
            End If
        Else
            Label1.Text = "One of the file doesn’t exists"
        End If
    End Sub
     
    Get the path of both files and check if both files exist. Create a instance of HashAlgorithm class using Create() method. Create two instances of FileStream class for both files and set FileMode open.  Call the ComputeHash method of HashAlgorithm instance for both FileStream instances and assign values to byte arrays. Compare results of both byte arrays.
     
  6. Start debugging and see the in browser.
  7. Change the content of one file and start debugging again to see result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to export DataTable to CSV file in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net