Compare File

In this chapter you will learn:

  1. Compare hash bytes of two files

Compare hash bytes of two files

using System;/*from  j a  va2 s  . c om*/
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("c:\\test.txt", FileMode.Open),
                fsB = new FileStream("c:\\test1.txt", FileMode.Open)){
                // Calculate the hash for the files.
                byte[] hashBytesA = hashAlg.ComputeHash(fsA);
                byte[] hashBytesB = hashAlg.ComputeHash(fsB);

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

Next chapter...

What you will learn in the next chapter:

  1. Copying A File by using FileInfo
Home » C# Tutorial » Stream
Stream classes
Text File Read
Text File write
Text File Create
Text File Append
Replace File Content
BinaryReader
BinaryWriter
FileStream Create
FileStream byte read and write
BufferedStream
Compare File
File Copy
File Copy with FileStream
MemoryStream
Object Serialization
String Writer