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

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);

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.


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);