programmatically showing differences between files

If you want to use our object model to show differences between two files, you'll want to look at the methods off of the Difference class in the Microsoft.TeamFoundation.VersionControl.Client namespace/assembly, specifically ones like DiffFiles, VisualDiffFiles, VisualDiffItems, etc.  Here's a simple example showing first a unified diff to stdout and then a visual diff that invokes in the appropriate registered third-party tool.

A sample run, showing a diff of 2 versions of the same file (2 different files would have been fine, too)

>difftwoversions jmanning-test $/60220-testing/test/foo.cs C11 $/60220-testing/test/foo.cs C12
Difference.DiffFiles - output to console
--- $/60220-testing/test/foo.cs;C11  (server)    3/7/2006 11:34 AM
+++ $/60220-testing/test/foo.cs;C12  (server)    3/7/2006 11:35 AM
@@ -1,6 +1,7 @@
 int foo(int bar)
+    int j = 5;
     int i = 1;

-    return bar + i;
+    return bar + i + j;
Press Enter to continue

Difference.VisualDiffItems - invoke registered third-party tool
[Note: the diff opened in in a separate window here]
Press Enter to continue

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;

namespace DiffTwoVersions
    class Program
        static void Main(string[] args)
            if (args.Length != 5)
                Console.Error.WriteLine("Usage: DiffItems servername sourceserveritem sourceversion targetserveritem targetversion");

            TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(args[0]);
            VersionControlServer vcs = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

            string sourcePath = args[1];
            VersionSpec sourceVersion = VersionSpec.ParseSingleSpec(args[2], tfs.AuthenticatedUserName);
            string targetPath = args[3];
            VersionSpec targetVersion = VersionSpec.ParseSingleSpec(args[4], tfs.AuthenticatedUserName);

            DiffOptions diffOptions = new DiffOptions();
            diffOptions.UseThirdPartyTool = false;
            diffOptions.OutputType = DiffOutputType.Unified;
            // Wherever we want to send our text-based diff output
            diffOptions.StreamWriter = new System.IO.StreamWriter(Console.OpenStandardOutput());

            Console.WriteLine("Difference.DiffFiles - output to console");
                                 Difference.CreateTargetDiffItem(vcs, sourcePath, sourceVersion, 0, sourceVersion), 
                                 Difference.CreateTargetDiffItem(vcs, targetPath, targetVersion, 0, targetVersion), 
            Console.WriteLine("Press Enter to continue");

            Console.WriteLine("Difference.VisualDiffItems - invoke registered third-party tool");
                                       Difference.CreateTargetDiffItem(vcs, sourcePath, sourceVersion, 0, sourceVersion), 
                                       Difference.CreateTargetDiffItem(vcs, targetPath, targetVersion, 0, targetVersion));
            Console.WriteLine("Press Enter to continue");

Comments (3)
  1. Buck Hodges blogs about TFS Standby application tier and database clustering.

    James Manning shows…

  2. Buck Hodges says:

    I recently had to put together a list of links to code samples.  This isn’t even close to comprehensive,…

  3. Buck Hodges says:

    I recently had to put together a list of links to code samples. This isn’t even close to comprehensive,

Comments are closed.

Skip to main content