Question

I am writing a vs2012 extension that will talk to TFS 2010 (though I would prefer if it could also work with tfs2012).

I need to invoke a compare operations on a file from the extension.

I want to use the default compare tool that is configured in visual studio at the moment of the innovation (because the user can configure a different compare tool).

I have the location of the file and I want to be able to invoke the following:

  • open the default compare.
  • open a compare with latest version
  • open a compare with workspace version
Était-ce utile?

La solution

Use IVsDifferenceService to invoke Visual Studio diff tool from your VSPackage:

private void Compare(string leftFile, string rightFile)
{
    var diffService = (IVsDifferenceService)GetService(typeof(SVsDifferenceService));
    if (diffService != null)
    {
        ErrorHandler.ThrowOnFailure(
            diffService.OpenComparisonWindow(leftFile, rightFile).Show()
            );
    }
}

To test it you need to set the workspace and download the file you want to compare:

    // TODO: add some error handling
    var tpc = new TfsTeamProjectCollection(new Uri("http://tfs.company.com:8080/tfs"));
    var vcs = tpc.GetService<VersionControlServer>();
    var workspace = vcs.GetWorkspace(Environment.MachineName, vcs.AuthorizedUser);

    string localItem = @"C:\workspace\project\somefile.cs";

    var folder = workspace.GetWorkingFolderForLocalItem(localItem);
    var item = vcs.GetItem(folder.ServerItem, VersionSpec.Latest);
    var latestItem = string.Format("{0}~{1}", localItem, item.ChangesetId);
    item.DownloadFile(latestItem);

    Compare(localItem, latestItem);

References:

using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top