I have two text files that represent settings of the same computer, however I am creating them at different times so the row of the date will always change.
Since this will be on multiple computers, I cannot rely on the row number, however I do know that the unnecessary info lines to check the difference is ;
.
How do I compare and say if the two files are identical efficiently but ignore lines that start with ;
in the compresence?
CodePudding user response:
You can use following LINQ query:
var file1Lines = File.ReadLines(path1).Where(l => !l.StartsWith(";"));
var file2Lines = File.ReadLines(path2).Where(l => !l.StartsWith(";"));
bool identical = file1Lines.SequenceEqual(file2Lines);
SequenceEqual
takes the order into account. If that's not desired use a HashSet<T>
:
bool identical = file1Lines.ToHashSet().SetEquals(file2Lines);
If you want to compare in a case insensitive manner, so treat A
same as a
, you have to pass StringComparer.OrdinalIgnoreCase
to ToHashSet
(or to SequenceEqual
).