Click here to Skip to main content
12,405,653 members (60,803 online)
Click here to Skip to main content
Add your own
alternative version

Stats

7.8K views
6 bookmarked
Posted

Using Built-in Visual Studio Diff Tool in VS Extension

, 5 Jan 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Mykola Tarasyuk
Software Developer (Senior)
Ukraine Ukraine
I am a software developer from Ukraine. In my spare time (this means "very rarely last years"), I am blogging about ASP.Net and web development.


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.

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