1

I have developed an add-in that displays all the files/folders in a particular directory. What i would like to do is, if i see a .sln file in the directory, on double click would like to open the solution in the current open Visual Studio solution explorer. I am using Visual Studio 2015.

System.Type type = Type.GetTypeFromProgID("VisualStudio.DTE.14.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(type); 
dte.MainWindow.Visible = true;
dte.Solution.Open(path); 

This particular code opens the solution in a fresh Visual Studio and not the current.

Thanks a lot in advance.

codeMORE
  • 21
  • 2
  • Possible duplicate of [Programmatically getting the current Visual Studio IDE solution directory from addins](http://stackoverflow.com/questions/2054182/programmatically-getting-the-current-visual-studio-ide-solution-directory-from-a) – Ilian Jan 27 '17 at 05:24
  • did you test my answer? whats your feedback? – S.Serpooshan Jan 27 '17 at 11:51

1 Answers1

2

According to this post, its better to get current solution this way:

The correct way to get DTE is very simple. In fact, your add-in already has reference to DTE in which it runs (that is, in which the solution is opened). It is stored in a global variable _applicationObject in your add-in connect class. It is set when your add-in starts in the OnConnection event handler.

So, we can run this to open a solution inside current Visual Studio instance:

_applicationObject.Solution.Open(@"D:\folder1\tets.sln");

The code will usually called inside your Add-Ins Exec method as following:

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
    handled = false;
    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    {
        if (commandName == "MyAddin1.Connect.MyAddin1")
        {
            _applicationObject.Solution.Open(@"D:\...\tets.sln"); // *** open solution inside current VS instance

            handled = true;
            return;
        }
    }
}
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
  • Removing my answer as yours is clearly better. Also flagged this question as a duplicate. – Ilian Jan 27 '17 at 05:27