title: How to use Team Foundation's library to calculate unified diff?
body: <p>I want to calculate a <a href="http://en.wikipedia.org/wiki/Diff#Unified_format" rel="nofollow">unified diff</a> comparing two documents. (The diff is to go in an email, and Wikipedia says unified diff is the best plain text diff format.) </p>
  
  <p>Team Foundation has a <a href="http://msdn.microsoft.com/en-us/library/6fd7dc73(v=vs.100).aspx" rel="nofollow">command line interface</a> do that</p>
  
  <pre><code>&gt; tf diff /format:unified alice.txt bob.txt
  - Alice started to her feet,
  + Bob started to her feet,
  </code></pre>
  
  <p>(Example files at <a href="https://gist.github.com/hickford/5656513" rel="nofollow">https://gist.github.com/hickford/5656513</a>)</p>
  
  <p>Brilliant, but I'd rather use a library than start an external process, for the usual reasons.</p>
  
  <p>Searching MSDN, I found Team Foundation has a .NET library <a href="http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client(v=vs.80).aspx" rel="nofollow"><code>Microsoft.TeamFoundation.VersionControl</code></a>. However, the documentation didn't give any examples of calculating a diff.</p>
  
  <p>How do I calculate a unified diff with the Team Foundation library?</p>
  
  <hr>
  
  <p>Edit: I tried the method
   <a href="http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.versioncontrol.client.difference.aspx" rel="nofollow"><code>Difference.DiffItems</code></a> but it didn't work—the file <code>diff.txt</code> was left empty.</p>
  
  <pre><code>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));
  </code></pre>