Skip to content
Related Articles

Related Articles

File Comparison Using C#

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Aug, 2022
View Discussion
Improve Article
Save Article

C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”, in which we can create files. Sometimes we need to do perform operations the on file. This operation can be anything from comparing files byte by byte or needing to check the dates or length of files.

For getting this we have some classes like FileStream, FileInfo which come under System.IO namespace.

To Perform File Comparison:

For comparison of files, we have several options either we can make a file stream and check the file byte by byte. C# has one FileInfo class which also can be used to compare the length or their creation date. FileInfo is a sealed class, which means it can not be inherited. Here we are going to use two classes FileStream and FileInfo for the comparisons.

public sealed class FileInfo : System.IO.FileSystemInfo

public class FileStream : System.IO.Stream

Example 1:

C#




// Use FileInfo Class in C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FileInfo1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            FileInfo f1 = new FileInfo(@"C:\test\test1.txt");
            FileInfo f2 = new FileInfo(@"C:\test\test2.txt");
             
            // checking the size of files
            if(f1.Length != f2.Length)
            {
                Console.WriteLine("File sizes are not equal");
            }
            else
            {
                Console.WriteLine("File sizes are equal");
            }
            //for getting Creation time
            Console.WriteLine("first file creation date is " + f1.CreationTime);
            Console.WriteLine("second file creation date is " + f2.CreationTime);
 
            //For getting parent directory
            Console.WriteLine("first file parent Directory is " + f1.Directory);
            Console.WriteLine("second file parent Directory is " + f2.Directory);
            Console.Read();
        }
    }
}


Output:

 

Example 2:

C#




// Use FileStream Class in C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace use_file_stream
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path1 = @"C:\test\test1.txt";
            string path2 = @"C:\test\test2.txt";
 
            using(FileStream fs1 = new FileStream(path1, FileMode.Open),
                  fs2 = new FileStream(path2, FileMode.Open))
            {
                int c1 = 0;
                int c2 = 0;
                do
                {
                    c1 = fs1.ReadByte();
                    c2 = fs2.ReadByte();
                }
                while (c1 == c2 && c1!=-1 && c2!=-1);
 
                if (c1 == c2)
                {
                    Console.WriteLine("Files are equal");
                }
                else{
                    Console.WriteLine("Files are not equal");
                }
                Console.ReadLine();
            }
            
        }
 
    }
}


Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!