Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A Generic Diff/Patch Utility written in C#

1.14/5 (15 votes)
27 May 2009CPOL 1   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.

License

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