Click here to Skip to main content
15,885,365 members
Articles / Programming Languages / C#
Tip/Trick

Using Built-in Visual Studio Diff Tool in VS Extension

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
19 Feb 2021CPOL 27.7K   9   5
An approach to call built-in Visual Studio commands from within Visual Studio extensions

Background

Once when I was reinventing the wheel (writing a custom JSON viewer/comparer), I encountered the following issue: I wanted to compare JSON data within a Visual Studio extension but I did not want to write my own comparer. I decided to use a built-in Visual Studio tool.

The tool is well known; you can call DiffFiles command in the Command window:

Tools.DiffFiles c:\file1 c:\file2

However, it was a puzzle for me how to execute this command from a Visual Studio extension without the Command window and without creation of another instance of Visual Studio. After spending few hours searching the Internet and making a number of tests, I came up with a simple solution which I'd like to describe here.

Solution

The main window of Visual Studio extensions having UI is inherited from Microsoft.VisualStudio.Shell.ToolWindowPane. This object has Package property which implements System.IServiceProvider interface. This interface provides access to the root object in Visual Studio COM. Here is the refined and simplified code to demonstrate the solution:

C#
public void CompareJsonData(IServiceProvider serviceProvider, string data1, string data2)
{
    var tempFolder = Path.GetTempPath();

    var tempFile1 = Path.Combine(tempFolder, Guid.NewGuid().ToString());
    File.WriteAllText(tempFile1, data1);

    var tempFile2 = Path.Combine(tempFolder, Guid.NewGuid().ToString());
    File.WriteAllText(tempFile2, data2);

    DTE dte = (DTE)serviceProvider.GetService(typeof(DTE));
    dte.ExecuteCommand("Tools.DiffFiles", 
    string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", tempFile1, tempFile2, 
    "1st JSON data", "2nd JSON data"));
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Ukraine Ukraine
I'm a software developer from Ukraine. I write about ASP.NET and other .NET related topics over at my blog

Comments and Discussions

 
GeneralMy vote of 5 Pin
Franc Morales20-Feb-21 19:36
Franc Morales20-Feb-21 19:36 
QuestionOne of this countless ... Pin
LightTempler20-Feb-21 8:41
LightTempler20-Feb-21 8:41 
PraiseNice tip! Pin
RickZeeland19-Feb-21 21:52
mveRickZeeland19-Feb-21 21:52 
QuestionWinMerge was not good enough ? Pin
Catalin Hatmanu5-Jan-15 14:39
Catalin Hatmanu5-Jan-15 14:39 
AnswerRe: WinMerge was not good enough ? Pin
Mykola Tarasyuk5-Jan-15 21:18
Mykola Tarasyuk5-Jan-15 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.