UweBaemayr
UweBaemayr

Reputation: 2329

Finding the home directory for a Visual Studio 2010 extension

I am making changes to a Visual Studio wizard that creates a project from a template, and needs to add a reference to an assembly to the project that also lives in the extension directory. So I need to set the <hintpath>.

I have not been able to figure out how a running VS extension can discover its extension directory, which is a directory name like this:

%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\myCompany\myExtension

Using System.Reflection.Assembly.GetExecutingAssembly().CodeBase yields:

"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\myCompany.myExtension\v4.0_1.0.0.0__936015a19c3638eb\myCompany.myExtension.dll"

Unfortunately, not helpful. Using GetCallingAssembly() is no better--it points at another directory in the MSIL_GAC.

Is there a Visual Studio interface that returns this information? I haven't been able to find it.

If that's not possible, is it at least possible to determine if the extension is running in the experimental instance vs. non-experimental? I could use that information to locate the extension directory.

Upvotes: 4

Views: 10577

Answers (2)

Hazza
Hazza

Reputation: 6581

Sorry for digging up an old answered question...

I was looking into a similar problem, and have implemented the Extension Manager, but decided this is just pretty awful, though if you do want to do use it these links will also help:

https://stackoverflow.com/a/24294185

http://blog.ninlabs.com/2011/04/auto-update-visual-studio-extensions/

However, I decided to look into how the CodeGenerator in Asp.Net.Scaffolding accessed template files. Turns out, very easily...

var path = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "MyPath");

Simples

Upvotes: 1

perelman
perelman

Reputation: 1777

Maybe look at IInstalledExtension.InstallPath. You can get an IInstalledExtension via an IVsExtensionManager.

Unfortunately, the message in the remarks suggests this is not the right way to do things:

Although this API supports the Extension Manager infrastructure, we recommend that you do not use it because it is subject to change.

EDIT: Here's the code:

    static IVsExtensionManager GetExtensionManager()
    {
        return myPackage.GetService(System.typeof(IVsExtensionManager)) as IVsExtensionManager;
    }
    static IInstalledExtension GetExtension(string identifier)
    {
        return GetExtensionManager().GetInstalledExtension(identifier);
    }
    static string GetExtensionDirectory(string identifier)
    {
        return GetExtension(identifier).InstallPath;
    }

The string identifier is whatever you put in the "ID" field of your extension's source.extension.vsixmanifest file. It defaults to the package GUID.

Upvotes: 4

Related Questions