5,446,542 members and growing! (16,156 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Intermediate

A Generic Diff/Patch Utility written in C#

By kevin delafield

Generic Diff/Patch Utility written in C#
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 24 Aug 2006
Updated: 24 Aug 2006
Views: 14,361
Bookmarked: 15 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 1.67 Rating: 1.61 out of 5
8 votes, 72.7%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
3 votes, 27.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

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

This article provides an implementation of a diff/patch 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.

The implementation also provides a command line interface with output similar to the UNIX diff command.

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();

Diff<string> diff = new Diff<string>(a, b, true);

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

To write the differences between file A and file B to the console,

diff.Write(Console.Out);

Patching an input or Converting A into B

To Patch a file by applying the diff edit script changes to an input, use the Patch Class

Patch<string> patch = new Patch<string>(a, diff.SES);

foreach (string line in patch.Lines)

{

Console.WriteLine(line);

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

kevin delafield



Location: United States United States

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralFile FormatsmemberSCADirector9:54 3 Jan '08  
GeneralDid you test this???memberDARKHalf2:41 4 Feb '07  
GeneralRe: Did you test this???memberAutoDeveloper4:05 17 May '07  
QuestionRe: Did you test this???membermrwizard82d118:42 26 Jul '07  
GeneralMore InformationmemberSteve Hansen23:25 24 Aug '06  
GeneralRe: More Informationmemberdiilbert3:30 25 Aug '06  
GeneralRe: More InformationmemberSteven Roebert12:10 25 Aug '06  
GeneralRe: More InformationmemberPIEBALDconsult4:34 15 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Aug 2006
Editor:
Copyright 2006 by kevin delafield
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project