Get the differences of two files

前端 未结 3 1756
我寻月下人不归
我寻月下人不归 2020-12-22 04:06

In .NET, I need a way to compare two files. I thought of a class, which represents a diff:

public enum DiffEntryState
{
    New,
    Removed,
    Changed
}           


        
相关标签:
3条回答
  • 2020-12-22 04:10

    For general case binary differencing, look at A Linear Time, Constant Space Differencing Algorithm by Randal C. Burns and Darrell D. E. Long. Also, Randal Burns' master's thesis, Differential Compression: A Generalized Solution For Binary Files, goes into more detail and provides pseudo-code for the algorithm.

    You might also get some useful ideas from About Remote Differential Compression and from Optimizing File Replication over Limited-Bandwidth Networks using Remote Differential Compression

    For text file differencing, I recommend starting with An O(ND) Difference Algorithm and Its Variations by Eugene W. Myers. This algorithm can be used to diff any two sequences. To compare two text files, generate sequences of hash codes (e.g., by calling string.GetHashCode()) for each line in each file. Then run those sequences (e.g., IList) through Myers' algorithm to find the shortest edit script (i.e., inserts and deletes) that will convert the first sequence into the second.

    I hope this helps. I'm the author of Diff.Net, and it uses Burns' algorithm for binary differencing and Myers' algorithm for text differencing. The source code for Diff.Net's libraries (Menees.Diffs and Menees.Diffs.Controls) are available under the Apache License, Version 2.0, and the references above should help you implement your own solution without having to start from scratch.

    0 讨论(0)
  • 2020-12-22 04:16

    There is no built-in functionality.

    So you have to compare the files byte by byte or use a library that does this for you.

    0 讨论(0)
  • 2020-12-22 04:18

    Take a look at Diff.NET,could be helpful .

    0 讨论(0)
提交回复
热议问题