How do you get the current solution directory from a VSPackage?

15,787

Solution 1

I found the answer to the specific question. The VisualStudio.DTE object can be retrieved via the GetService() method as follows:

// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);

Solution 2

You can get a DTE object from one of these functions:

public static DTE GetCurrentDTE(IServiceProvider provider)
{
    /*ENVDTE. */DTE vs = (DTE)provider.GetService(typeof(DTE));
        if (vs == null) throw new InvalidOperationException("DTE not found.");
    return vs;
}

public static DTE GetCurrentDTE()
{
    return GetCurrentDTE(/* Microsoft.VisualStudio.Shell. */ServiceProvider.GlobalProvider);
}

After that, you can get active Solution from DTE.Solution and Solution path from DTE.Solution.Path property.

Solution 3

If using the IVsSolution interface, you can use GetSolutionInfo to obtain the path of the solution, the solution filename, and the solution user options (SUO) filename:

this.solution.GetSolutionInfo(
    out string solutionDirectory,
    out string solutionFile,
    out string userOptsFile);
Share:
15,787
Dave Clemmer
Author by

Dave Clemmer

I enjoy coding like an excellent beer. My particular passion and experience lies in the realm of modeling and code generation. In the late 80s and early 90s, I was involved in early modeling and code generation tools that reached the marketplace, including a tool that modeled FORTRAN programs and generated FORTRAN for parallel supercomputer architectures, and a tool that managed Shlaer-Mellor models and generated C++ code. Over the years, I have applied Shlaer-Mellor, UML, and custom modeling and various code generation techniques to greatly benefit the development of enterprise applications. My current passion and endeavor is to foster the evolution of model oriented development. In particular, I am passionate about evolving the Mo+ model oriented programming language and related model driven development tools, with as much community input as possible to achieve higher levels in the ability to create and maintain code. The open source site is at moplus.codeplex.com, and the Mo+ membership site is at modelorientedplus.com.

Updated on June 28, 2022

Comments

  • Dave Clemmer
    Dave Clemmer almost 2 years

    Following is how you would get the current solution directory from an add-in:

    _applicationObject = (DTE2)application;  // retrieved from OnConnection method
    string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
    

    How would you do this via a VSPackage?

    I'm migrating a visual studio add-in to a VSPackage as I'm intending to add some user controls that require a deeper integration with the IDE.

    I found some good references on the relative merits of add-ins vs integration packages such as: http://nayyeri.net/visual-studio-addin-vs-integration-package-part-1

    And some good tutorials on msdn on VSPackages such as: http://msdn.microsoft.com/en-us/library/cc138589.aspx

    I haven't found a good reference yet (on msdn or otherwise) on how the higher level interfaces in add-ins (such as DTE) map to lower level interfaces in VSPackages.

    Any good references out there to help with general mapping from add-in interfaces to VSPackage interfaces?

  • Nicolas Fall
    Nicolas Fall over 13 years
    Does this code have any issues when multiple copies of visual studio are running?
  • Dave Clemmer
    Dave Clemmer over 13 years
    I have used with with multiple instances of VS in standard and hibernate mode and haven't run into any problems with getting the correct current solution. Earlier approaches I tried DID have trouble getting the correct instance, such as: DTE dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject(‌​"VisualStudio.DTE"); string solutionDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
  • Ali Mst
    Ali Mst almost 12 years
    Nice code. Now I know how to get he DTE Object. But how do I get an IServiceProvider (When not writing a Visual Studio extension).