13

我想计算比较两个文档的统一差异。(差异是通过电子邮件发送的,维基百科说统一差异是最好的纯文本差异格式。)

Team Foundation 有一个命令行界面可以做到这一点

> tf diff /format:unified alice.txt bob.txt
- Alice started to her feet,
+ Bob started to her feet,

(示例文件位于https://gist.github.com/hickford/5656513

太棒了,但出于通常的原因,我宁愿使用库而不是启动外部进程。

搜索 MSDN,我发现 Team Foundation 有一个 .NET 库Microsoft.TeamFoundation.VersionControl。但是,该文档没有给出任何计算差异的示例。

如何使用 Team Foundation 库计算统一差异?


编辑:我尝试了该方法 Difference.DiffItems,但没有奏效——文件diff.txt为空。

var before = @"c:\alice.txt";
var after = @"c:\bob.txt";

var path = @"c:\diff.txt";
using (var w = new StreamWriter(path))
{
    var options = new DiffOptions();
    options.OutputType = DiffOutputType.Unified;
    options.StreamWriter = w;

    Difference.DiffFiles(
    before, FileType.Detect(before, null),
    after, FileType.Detect(after, null),
    options );
}

Console.WriteLine(File.ReadAllText(path));
4

2 回答 2

3

请尝试DiffSegment

        var diff = Difference.DiffFiles(
        before, FileType.Detect(before, null),
        after, FileType.Detect(after, null),
        options);

        while (diff != null){                
            //DO What you like with the diff(s)
            diff = diff.Next;
        }
于 2013-05-22T08:37:23.183 回答
0

猜猜这是不可能的:(你只能从命令行使用tf.exe.

于 2013-05-28T10:49:38.700 回答