Click here to Skip to main content
15,879,184 members
Articles / Programming Languages / C#

A Generic Diff/Patch Utility written in C#

Rate me:
Please Sign up or sign in to vote.
1.14/5 (15 votes)
27 May 2009CPOL 58K   641   27   13
Generic Diff/Patch Utility written in C#

Download Diff2.zip - 7.82 KB

Introduction

Have you ever wanted or needed a diff utility optimized for the .NET platform.

This article provides an implementation of a diff utility written in C#.

It operates over generics, so you can provide lists of anything to be diffed or patched,
including strings (such as lines from files), int, objects, or even bytes.

Example of using the Diff class to diff 2 text files

List<string> a = new List<string>();

List<string> b = new List<string>();

StreamReader fs1 = new StreamReader(argv[0]);

StreamReader fs2 = new StreamReader(argv[1]);

string line;

while ((line = fs1.ReadLine()) != null)

{

    a.Add(line);

}

while ((line = fs2.ReadLine()) != null)

{

    b.Add(line);

}

fs1.Dispose();

fs2.Dispose();

Item[] diff_items = Diff2.Diff<string>(a.ToArray(), b.ToArray());

This performs the diff. The final parameter indicates whether the actual differences should be included in the edit script when the diff is performed.

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
gam6it28-Jul-11 22:33
gam6it28-Jul-11 22:33 
GeneralSeems very similar to another one. Pin
Vladimir Vasiltsov17-Feb-10 10:03
Vladimir Vasiltsov17-Feb-10 10:03 
GeneralFile Formats Pin
SCADirector3-Jan-08 8:54
SCADirector3-Jan-08 8:54 
QuestionDid you test this??? Pin
DARKHalf4-Feb-07 1:41
DARKHalf4-Feb-07 1:41 
AnswerRe: Did you test this??? Pin
AutoDeveloper17-May-07 3:05
AutoDeveloper17-May-07 3:05 
QuestionRe: Did you test this??? Pin
mrwizard82d126-Jul-07 17:42
mrwizard82d126-Jul-07 17:42 
AnswerRe: Did you test this??? Pin
kevin delafield27-May-09 22:27
kevin delafield27-May-09 22:27 
GeneralRe: Did you test this??? Pin
mrwizard82d128-May-09 4:42
mrwizard82d128-May-09 4:42 
GeneralMore Information Pin
Steve Hansen24-Aug-06 22:25
Steve Hansen24-Aug-06 22:25 
GeneralRe: More Information Pin
diilbert25-Aug-06 2:30
diilbert25-Aug-06 2:30 
GeneralRe: More Information Pin
Steven Roebert25-Aug-06 11:10
Steven Roebert25-Aug-06 11:10 
GeneralRe: More Information Pin
PIEBALDconsult15-Dec-06 3:34
mvePIEBALDconsult15-Dec-06 3:34 
GeneralRe: More Information Pin
kevin delafield27-May-09 22:27
kevin delafield27-May-09 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.