C# Program to Compare the Content of Two Files Using Streareader Class

You are currently viewing C# Program to Compare the Content of Two Files Using Streareader Class

C# program to compare the content of two files using StreaReader class

//C# program to compare the content of 
//two files using StreamReader class.

using System;
using System.Threading;
using System.IO;

class Demo
{
    public static string Read(string filename)
    {
        string data;

        try
        {
            FileStream s = new FileStream(filename, FileMode.Open);
            StreamReader r = new StreamReader(s);

            data = r.ReadToEnd();
            r.Close();
            s.Close();
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine(e.Message);
            return null;
        }
        return data;
    }
    static void Main(string[] arg)
    {
        string data1 = "";
        string data2 = "";

        data1 = Read("d:/file1.txt");
        data2 = Read("d:/file2.txt");

        if (data1 == null || data2 == null)
            return;

        if (data1 == data2)
            Console.WriteLine("Content of files is similar");
        else
            Console.WriteLine("Content of files is not similar");
    }
}
Content of files is similar
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply