TFS API Part 53 – Using Visual Studio Compare Tool

28/11/2013

tags:
no comments

This is not really TFS API Post but because I’m going to show how to use Visual Studio Compare Tool I decided this is part of TFS Eco system and it should be part of TFS API Series.

As you know in Visual Studio 2012 the Compare Tool had major improvements and because it’s such a great compare tool why not using it outside Visual Studio.

Download Demo Project

SNAGHTML5ac3ad

image

The first step is locate which version of Visual Studio installed on your machine:

Identify Installed Visual Studio Versions

public static IEnumerable<DiffTool> LocateDiffTools()
{
    var diffTools = new List<DiffTool>();

    // Visual Studio 2013/2012 has a built-in diff tool
    using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\12.0\Setup\VS"))
    {
        if (regKey != null)
        {
            var devenvPath = (string)regKey.GetValue("EnvironmentPath");
            if (File.Exists(devenvPath))
            {
                diffTools.Add(new DiffTool("Visual Studio 2013", devenvPath, "/diff %1 %2 %6 %7"));
            }
        }
    }

    // Visual Studio 2013/2012 has a built-in diff tool
    using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\11.0\Setup\VS"))
    {
        if (regKey != null)
        {
            var devenvPath = (string)regKey.GetValue("EnvironmentPath");
            if (File.Exists(devenvPath))
            {
                diffTools.Add(new DiffTool("Visual Studio 2012", devenvPath, "/diff %1 %2 %6 %7"));
            }
        }
    }

    // Older versions should have a diffmerge.exe tool in the IDE path.
    using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VS"))
    {
        if (regKey != null)
        {
            var idePath = (string)regKey.GetValue("EnvironmentDirectory");
            var diffmergePath = System.IO.Path.Combine(idePath, "diffmerge.exe");
            if (File.Exists(diffmergePath))
            {
                diffTools.Add(new DiffTool("Visual Studio 2010", diffmergePath, "%1 %2 %6 %7 /ignorespace"));
            }
        }
    }
    using (var regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS"))
    {
        if (regKey != null)
        {
            var idePath = (string)regKey.GetValue("EnvironmentDirectory");
            var diffmergePath = System.IO.Path.Combine(idePath, "diffmerge.exe");
            if (File.Exists(diffmergePath))
            {
                diffTools.Add(new DiffTool("Visual Studio 2008", diffmergePath, "%1 %2 %6 %7 /ignorespace"));
            }
        }
    }

    return diffTools;
}

After you find devenv.exe file you just need to execute a command line action passing the right arguments:

Basically checking you’ve selected Left and Right files (Source and Target), select the desire Compare Tool (Visual Studio version) and launch the command line.

SNAGHTML855034

private void btnCompare_Click(object sender, RoutedEventArgs e)
{
    if (string.IsNullOrEmpty(txtLeft.Text) || string.IsNullOrEmpty(txtRight.Text)) return;
    if (!File.Exists(txtLeft.Text) || !File.Exists(txtRight.Text)) return;
if (toolsList.SelectedItem == null) return;

DiffTool diffTool = (DiffTool)toolsList.SelectedItem;

diffTool.Launch(txtLeft.Text, txtRight.Text,
Path.GetFileName(txtLeft.Text), Path.GetFileName(txtRight.Text));
}

Download Demo Project

Enjoy

Add comment
facebook linkedin twitter email

Leave a Reply