31. Finding duplicate files

One way to determine whether two files are identical is to compare the files byte-by-byte. The following method uses this approach to determine whether two files are identical:

// Return true if the files are identical.public static bool FilesAreIdentical(FileInfo fileinfo1,    FileInfo fileinfo2){    byte[] bytes1 = File.ReadAllBytes(fileinfo1.FullName);    byte[] bytes2 = File.ReadAllBytes(fileinfo2.FullName);    if (bytes1.Length != bytes2.Length) return false;    for (int i = 0; i < bytes1.Length; i++)        if (bytes1[i] != bytes2[i]) return false;    return true;}

This method uses the File class's ReadAllBytes method to read the two files into byte arrays. If the arrays have different lengths, then the files are not identical, so ...

Get Improving your C# Skills now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.