比较两个文本文件并显示相同之处

Compare two text files and show what is the SAME

我在比较两个文件时需要帮助。我能够打印出两者之间的区别,但我无法弄清楚当两者错误时如何打印两者之间的相同之处。任何人都可以帮助我吗?提前致谢!

    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.");
        }

    }

首先,我认为您的代码中可能存在错误。 'fileOld' 包含旧文件的所有内容,而 'OldTxt' 仅包含新文件中不存在的旧文件文本。在我回答之后,请参阅下面的一些代码清理想法。

我认为您正在寻找 Intersect,其中 returns 两个 IEnumerable 的共同项:

var commonItems = fileOld.Intersect(fileNew);

或者,由于您已经有了在 difFromNew 中捕获的差异列表,您可以再次使用 Except

var difFromNew = fileOld.Except(fileNew); // Note I fixed what I think is a bug here
var commonItems = fileOld.Except(difFromNew);

一些潜在的错误:

  1. SequenceEqual 表示不仅项目相同,而且顺序相同。如果您确实关心顺序,那么这是合适的。问题是这会使显示差异变得困难,因为您用来比较列表的其他方法(ExceptIntersect 关心顺序,仅判断项目是否存在。因此,如果 fileOld 包含项目 cat | dogfileNew 包含项目 dog | cat,那么它们将不相等,但您也无法显示差异(fileOld.Except(fileNew) 将包含 0 项)。
  2. 在您的 diffFromNew 列表中,您正在使用 OldTxt,这是来自旧文件的 独特的 文本,并执行 Except 反对 NewTxt,这是来自新文件的 unique 文本。这两者之间不能有重叠 - 实际上 OldTxt 已经默认包含 diffFromNew

这是获取所需列表的一种方法:

var oldFilePath = "c:\oldFile.txt";
var newFilePath = "c:\newFile.txt";

var oldFileTxt = File.ReadLines(oldFilePath);
var newFileTxt = File.ReadLines(newFilePath);

// Determine if they're equal by checking if there are not any unique items
var filesAreSame = !oldFileTxt.Except(newFileTxt, StringComparer.OrdinalIgnoreCase).Any();

var commonItems = oldFileTxt.Intersect(newFileTxt, StringComparer.OrdinalIgnoreCase);
var uniqueOldItems = oldFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);
var uniqueNewItems = newFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);

下面是代码经过这些更改后的样子:

if (filesAreSame)
{
    MessageBox.Show("They match!");
}
else
{
    var commonItems = oldFileTxt.Intersect(newFileTxt, StringComparer.OrdinalIgnoreCase);
    var uniqueOldItems = oldFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);
    var uniqueNewItems = newFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);

    var notEqualsFileText = new StringBuilder();
    if (uniqueOldItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {oldFilePath} that are not in {newFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueOldItems));
    }
    if (uniqueNewItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {newFilePath} that are not in {oldFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueNewItems));
    }

    File.WriteAllText(notEqualFilePath, notEqualsFileText.ToString());

    var equalsFileText = new StringBuilder();
    if (commonItems.Any())
    {
        equalsFileText.AppendLine(
            $"Entries that are common in both {newFilePath} and {oldFilePath}:");
        equalsFileText.AppendLine(string.Join(Environment.NewLine, commonItems));
    }
    else
    {
        equalsFileText.AppendLine(
            $"There are no common entries in both {newFilePath} and {oldFilePath}.");
    }

    File.WriteAllText(equalFilePath, equalsFileText.ToString());

    MessageBox.Show("The files 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.");
}