/// <summary> /// Accepts two strings that represents two files to compare. /// </summary> /// <param name="filePath1">The file1.</param> /// <param name="filePath2">The file2.</param> /// <param name="bufferSize">The size of buffer.</param> /// <param name="posOfFirstDiffByte">Position of the first diff byte or -1 if files are equal.</param> /// <returns>Return true indicates that the contents of the files are the same. Return false if the files are different.</returns> public static bool CompareFiles(string filePath1, string filePath2, int bufferSize, out long posOfFirstDiffByte) { posOfFirstDiffByte = -1; // Determine if the same file was referenced two times. if (0 == string.Compare(filePath1, filePath2, true)) { // Return true to indicate that the files are the same. return(true); } // If some of the files does not exists then assume they are different. if (!(File.Exists(filePath1) && File.Exists(filePath2))) { return(false); } using (var stream1 = new FileStream(filePath1, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var stream2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read, FileShare.Read)) { return(stream1.CompareTo(stream2, bufferSize, out posOfFirstDiffByte)); } } }