Test Two Files for Equality - CSharp File IO

CSharp examples for File IO:File

Description

Test Two Files for Equality

Demo Code


using System;//from ww  w . ja v  a2s  .com
using System.IO;
using System.Security.Cryptography;

    static class MainClass
    {
        static void Main(string[] args)
        {
            using (HashAlgorithm hashAlg = HashAlgorithm.Create())
            {
                using (FileStream fsA = new FileStream("Main.cs", FileMode.Open), fsB = new FileStream("Main.exe", FileMode.Open))
                {
                    byte[] hashBytesA = hashAlg.ComputeHash(fsA);
                    byte[] hashBytesB = hashAlg.ComputeHash(fsB);

                    if (BitConverter.ToString(hashBytesA) == BitConverter.ToString(hashBytesB))
                    {
                        Console.WriteLine("Files match.");
                    }
                    else
                    {
                        Console.WriteLine("No match.");
                    }
                }
            }
        }
    }

Result


Related Tutorials