Finding the home directory for a Visual Studio 2010 extension

陌路散爱 提交于 2019-12-22 08:40:59

问题


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.


回答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.




回答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



来源:https://stackoverflow.com/questions/8762062/finding-the-home-directory-for-a-visual-studio-2010-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!