I'm trying to compare two different text files and write in a new file the lines which are different. What I have so far writes the differences between the two files, I'm wondering what I would need to add to the code so I can write the lines as well. For example:
text1:
a
bc
d
_
f
text2:
a
bcd
d
e
_
what would be outputted with my code is:
_
d
_
e
f
what I'd like is:
line 2: d
line 4: e
line 5: f
hopefully this makes sense, here is my code:
private void button_compare_Click(object sender, EventArgs e)
{
String directory = @"C:\.......\";
String[] linesA = File.ReadAllLines(Path.Combine(directory, "test1.txt"));
String[] linesB = File.ReadAllLines(Path.Combine(directory, "test2.txt"));
IEnumerable<String> onlyB = linesB.Except(linesA);
File.WriteAllLines(Path.Combine(directory, "Result2.txt"), onlyB);
}
EDIT
I figured out my initial issue thanks to the excellent people who responded below. Out of curiosity I wanted to take it a bit further....
Suppose random lines in each file differed by one word. ex:
text1:
line 3: hello how are you
text2:
line 3: hi how are you
How would you do it such that the output file simply has the word that has been changed? eg
output file:
line 3: hello