我想计算一个比较两个文档的unified diff。 (差异是用电子邮件发送的,维基百科说统一差异是最好的纯文本差异格式。)
Team Foundation有一个command line interface做那个
> 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));
答案 0 :(得分: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;
}
答案 1 :(得分:0)
猜猜不可能:(您只能通过命令行tf.exe
执行此操作。