Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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!
Posted

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 10:31am    
5ed.
—SA
Mehdi Gholam 12-Jan-16 3:28am    
Thanks Sergey!
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:
C#
// 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");
    }
}
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.
 
Share this answer
 
Comments
Kasthuri Gunabalasingam 12-Jan-16 3:22am    
thankyouu!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900