如何获得C#中两个文件之间的差异 [英] How to get the difference between two files in C#

查看:97
本文介绍了如何获得C#中两个文件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件A1.txt和A2.txt. A1.txt是A2.txt的早期版本,并且某些行已添加到A2.如何获得添加到A2的新行?

我只希望添加新行,而不想要A1中但在A2中删除的行.

请提出一种方法!

I have two files A1.txt and A2.txt. A1.txt is previous version of A2.txt and some lines have been added to A2. How can I get the new lines that are added to A2?

I just want the new lines added and dont want the lines which were in A1 but deleted in A2.

Please suggest a way to do this!

推荐答案

从这里开始:

Mehdi Gholam指代您的文章to是深入"分析差异的极好来源.

为了找到一个文本文件中的哪些字符串不是另一个文本文件中的字符串不是的简单目的,您可以使用一种非常简单的技术:
The article that Mehdi Gholam refers you to is an excellent source for "going deep" into analyzing differences.

For the simple purpose of finding what strings in one Text File are not in the strings in another Text File, you can use a pretty simple technique:
// required
using System.IO;
using System.Linq;

string originalPath = @""; // path to valid .txt File
string revisionPath = @""; // path to valid .txt File

private void SomeButton_Click(object sender, EventArgs e)
{
    string[] InRevisionNotInOriginal = LineDiff(originalPath, revisionPath).ToArray();

    string[] InOriginalNotInRevision = LineDiff(revisionPath, originalPath).ToArray();
}

public static IEnumerable<string> LineDiff(string originalPath, string revisionPath)
{
    if (File.Exists(originalPath) && File.Exists(revisionPath))
    {
        return File.ReadAllLines(revisionPath).Except(File.ReadAllLines(originalPath));
    }
    else
    {
        throw new FileNotFoundException("Bad File Path");
    }
}

当您要检测何时将source1中的元素移到了source2中的不同位置时,分析差异"就变得很有趣.并且,当您要检测重复值元素总数的变化时.

Where analyzing "difference" gets interesting is when you want to detect when an element in source1 has been moved to a different position in source2; and, when you want to detect changes in the total number of duplicate value elements.


这篇关于如何获得C#中两个文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆