查找Visual Studio 2010扩展的主目录 [英] Finding the home directory for a Visual Studio 2010 extension

查看:190
本文介绍了查找Visual Studio 2010扩展的主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对Visual Studio向导进行更改,该向导从模板创建项目,并且需要向该程序集添加对程序集的引用,该程序集也位于扩展目录中.所以我需要设置<hintpath>.

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

我无法弄清正在运行的VS扩展如何发现其扩展目录,该目录是这样的目录:

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

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

使用System.Reflection.Assembly.GetExecutingAssembly().CodeBase会产生:

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

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

不幸的是,没有帮助.使用GetCallingAssembly()并不更好,它指向MSIL_GAC中的另一个目录.

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

是否有一个Visual Studio界面返回此信息?我找不到它.

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.

推荐答案

也许看看 IInstalledExtension.InstallPath .您可以通过 IVsExtensionManager .

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:

尽管此API支持扩展管理器基础结构,但我们建议您不要使用它,因为它可能会发生更改.

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

这是代码:

    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;
    }

string identifier是您在扩展程序source.extension.vsixmanifest文件的"ID"字段中输入的内容.默认为软件包GUID.

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.

这篇关于查找Visual Studio 2010扩展的主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆