Finding the home directory for a Visual Studio 2010 extension

10,310

Solution 1

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.

Solution 2

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

Share:
10,310
UweBaemayr
Author by

UweBaemayr

Senior Principal Software Developer at Micro Focus, by way of Liant Software, which was previously Ryan McFarland Corporation.

Updated on August 22, 2022

Comments

  • UweBaemayr
    UweBaemayr over 1 year

    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.

  • UweBaemayr
    UweBaemayr about 12 years
    Thanks! I will give that a try with some sanity checking. I can always let it fall back to the kludge I'm using now.
  • Kiquenet
    Kiquenet almost 10 years
    Maybe useful return Package.GetGlobalService(typeof(IVsExtensionManager)) as IVsExtensionManager;