Click here to Skip to main content
12,998,875 members (31,375 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 5 Jan 2015

Using Built-in Visual Studio Diff Tool in VS Extension

, 5 Jan 2015
Rate this:
Please Sign up or sign in to vote.
An approach to call built-in Visual Studio commands from within Visual Studio extensions


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.


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:

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));
    string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", tempFile1, tempFile2, 
    "1st JSON data", "2nd JSON data"));


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


About the Author

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

You may also be interested in...


Comments and Discussions

QuestionWinMerge was not good enough ? Pin
Catalin Hatmanu5-Jan-15 14:39
memberCatalin Hatmanu5-Jan-15 14:39 
AnswerRe: WinMerge was not good enough ? Pin
Mykola Tarasyuk5-Jan-15 21:18
memberMykola 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 5 Jan 2015
Article Copyright 2015 by Mykola Tarasyuk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid