Quote:
create a File-Compare function in Visual C#
Create a new Visual C# Windows Application project. By default, Form1 is created.
Add two textbox controls to the form.
Add a command button to the form.
On the View menu, click Code.
Add the following USING statement to the Form1 class:
using System.IO
Add the following method to the Form1 class:
private bool FileCompare(string file1, string file2)
{
int file1byte;
int file2byte;
FileStream fs1;
FileStream fs2;
if (file1 == file2)
{
return true;
}
fs1 = new FileStream(file1, FileMode.Open);
fs2 = new FileStream(file2, FileMode.Open);
if (fs1.Length != fs2.Length)
{
fs1.Close();
fs2.Close();
return false;
}
do
{
file1byte = fs1.ReadByte();
file2byte = fs2.ReadByte();
}
while ((file1byte == file2byte) && (file1byte != -1));
fs1.Close();
fs2.Close();
return ((file1byte - file2byte) == 0);
}
Paste the following code in the Click event of the command button:
private void button1_Click(object sender, System.EventArgs e)
{
if (FileCompare(this.textBox1.Text, this.textBox2.Text))
{
MessageBox.Show("Files are equal.");
}
else
{
MessageBox.Show("Files are not equal.");
}
}
Save and then run the sample.
Supply the full paths to the two files in the textboxes, and then click the command button.