65.9K
CodeProject is changing. Read more.
Home

A Generic Diff/Patch Utility written in C#

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.14/5 (11 votes)

Aug 25, 2006

CPOL
viewsIcon

59128

downloadIcon

645

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.