Finding the home directory for a Visual Studio 2010 extension
Asked Answered
R

2

4

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.

Rhythmist answered 6/1, 2012 at 17:43 Comment(0)
H
4

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.

Hannahannah answered 12/2, 2012 at 9:35 Comment(2)
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.Rhythmist
Maybe useful return Package.GetGlobalService(typeof(IVsExtensionManager)) as IVsExtensionManager;Volny
C
1

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

Coincidental answered 16/9, 2014 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.