问题描述
我有两个文件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!
推荐答案
// 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#中两个文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!