Compares the specified file1. This method accepts two strings the represent two files to compare. : File Command « File Stream « C# / C Sharp






Compares the specified file1. This method accepts two strings the represent two files to compare.

     
        

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.AccessControl;
using System.IO;
using System.Security.Principal;

namespace Td.Additional.IO
{
    /// <summary>
    /// File facilities.
    /// </summary>
    public static class File
    {
        /// <summary>
        /// Compares the specified file1.
        /// This method accepts two strings the represent two files to 
        /// compare.
        /// </summary>
        /// <param name="file1">The file1.</param>
        /// <param name="file2">The file2.</param>
        /// <returns>True, if files are equal, otherwise false.</returns>
        private static bool Compare(string file1, string file2)
        {
            int file1byte;
            int file2byte;
            FileStream fs1;
            FileStream fs2;

            // Determine if the same file was referenced two times. 
            if (file1 == file2)
            {
                // Return true to indicate that the files are the same. 
                return true;
            }

            // Open the two files. 
            fs1 = new FileStream(file1, FileMode.Open);
            fs2 = new FileStream(file2, FileMode.Open);

            // Check the file sizes. If they are not the same, the files 
            // are not the same. 
            if (fs1.Length != fs2.Length)
            {
                // Close the file 
                fs1.Close();
                fs2.Close();

                // Return false to indicate files are different 
                return false;
            }

            // Read and compare a byte from each file until either a 
            // non-matching set of bytes is found or until the end of 
            // file1 is reached. 
            do
            {
                // Read one byte from each file. 
                file1byte = fs1.ReadByte();
                file2byte = fs2.ReadByte();
            }
            while ((file1byte == file2byte) && (file1byte != -1));

            // Close the files. 
            fs1.Close();
            fs2.Close();

            // Return the success of the comparison. "file1byte" is 
            // equal to "file2byte" at this point only if the files are 
            // the same. 
            return ((file1byte - file2byte) == 0);
        }

    }
}

   
    
    
    
    
  








Related examples in the same category

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.File Renamer
11.Format File Size
12.Is File Existed
13.Is File readable or writeable
14.Replace in file
15.Send a file to the recycle bin
16.Copy file and folder
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder