c# Compare two text files and generate a new one with differences

假如想象 提交于 2019-12-01 10:45:40

Very simple approach, assuming that similar means equal:

var file1Lines = File.ReadLines(file1Path);
var file2Lines = File.ReadLines(file2Path);
IEnumerable<String> inFirstNotInSecond = file1Lines.Except(file2Lines);
IEnumerable<String> inSecondNotInFirst = file2Lines.Except(file1Lines);

You can use foreach to enumerate the lines.

You can use this diff library from Google. Look at the diff_main method that takes the 2 strings and returns a list of differences.

If the output is in the same order, compare lines directly. You may need to skip line when the value is missing in the other file.

If, however, the output is not the same, then you might need to load the files into memory and look up the relevant inventory item from one file in to the other. Then do whatever you need when not-found or different.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!