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 . . .
Table of Contents