我需要帮助来比较两个文件。我能够打印出两者之间的区别,但我无法弄清楚当它们错误时如何打印两者之间的相同之处。任何人都可以帮助我吗?提前致谢!
private void button2_Click(object sender, EventArgs e)
{
try
{
//Reads files line by line
var fileOld = File.ReadLines(Old);
var fileNew = File.ReadLines(New); //Ignores cases in files
IEnumerable<String> OldTxt = fileOld.Except(fileNew, StringComparer.OrdinalIgnoreCase);
IEnumerable<String> NewTxt = fileNew.Except(fileOld, StringComparer.OrdinalIgnoreCase);
FileCompare compare = new FileCompare();
bool areSame = OldTxt.SequenceEqual(NewTxt);
if (areSame == true)
{
MessageBox.Show("They match!");
}
else if(areSame == false)
{
// Find the set difference between the two files.
// Print to Not Equal.txt
var difFromNew = (from file in OldTxt
select file).Except(NewTxt);
using (var file = new StreamWriter(NotEqual))
{
foreach (var notEq in difFromNew)
{
file.WriteLine(notEq.ToString() + "\n", true);
}
}
MessageBox.Show("They are not the same! Please look at 'Not Equal.txt' to see the difference. Look at 'Equal.txt' to see what is the same at this time.");
}
}
catch
{
MessageBox.Show("'NewConvert.txt' or 'OldConvert.txt' files does not exist.");
}
}