How to Compare CSharp, Java, Python, Ruby files

You are working in an IT project on one product. You need to compare two CS files and accept or reject the found changes found in the specified range. With this action, you will merge the two files into one with the edits from different developers.

Files
Source
Target

GroupDocs.Comparison provides the ability to compare two files in CS format(or any other supported file formats).

The following are the steps to compare two CS files

ApplyChangeOptions class:

  • getChanges - List of changes that must be applied (or not) to the resulting document;

The following code samples demonstrate how to compare two CS files and accept or reject detected changesin a specific range.

Comparer comparer = new Comparer(SOURCE_FILE); // NOTE: Put here actual path to source document
try {
    comparer.add(TARGET_FILE); // NOTE: Put here actual path to target document
    comparer.compare();
    ChangeInfo[] changes = comparer.getChanges();
    for (int n = 0; n < 5; n++) {
        changes[n].setComparisonAction(ComparisonAction.REJECT);
    }
 
    for (int n = 6; n < 17; n++) {
        changes[n].setComparisonAction(ComparisonAction.ACCEPT);
    }
    comparer.applyChanges(RESULT_FILE, new SaveOptions(), new ApplyChangeOptions(changes));
} finally {
    comparer.dispose();
}

As a result, we get a merged CS file where the deleted elements are marked in red, the added – in blue, and the modified – in green.

Also, you will receive a file in HTML format with changed places in the code.

Result File