Getting the current EnvDTE or IServiceProvider when NOT coding an Addin

Posted by Vaccano on Stack Overflow See other posts from Stack Overflow or by Vaccano
Published on 2012-06-02T17:48:30Z Indexed on 2012/06/05 22:40 UTC
Read the original article Hit count: 639

I am coding up some design time code. I want to use this snippet: (Found here)

var dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE));
if (dte != null)
{
    var solution = dte.Solution;
    if (solution != null)
    {
        string baseDir = Path.GetDirectoryName(solution.FullName);
    }
}

Problem is that this does not compile. (GetService is not a known method call) I tried adding Microsoft.VisualStudio.Shell (and Microsoft.VisualStudio.Shell.10.0) but it did not help.

In looking around on the internet I found that you need a IServiceProvider to call this.

But all the examples that show how to get an IServiceProvider use a EnvDTE.

So, to get the current EnvDTE I need IServiceProvider. But to get an IServiceProvider I need an EnvDTE. (There is a hole in my bucket...)

So, here is my question:

In a normal WPF Application, how can I get the current instance of EnvDTE?

NOTE: I am not looking for any old instance of EnvDTE. I need the one for my current Visual Studio instance (I run 3-4 instances of Visual Studio at a time.)

© Stack Overflow or respective owner

Related posts about .NET

Related posts about visual-studio-2010