简体   繁体   English

如何从VSPackage获取当前运行的Visual Studio安装路径

[英]How get the current running Visual Studio installation path from VSPackage

I've created a VSPackage which should copy some XML schema files to Visual Studio's installation path: %VS install path% \\Xml\\Schemas . 我创建了一个VSPackage ,它应该将一些XML模式文件复制到Visual Studio的安装路径: %VS install path% \\Xml\\Schemas

I have multiple Visual Studios installed on my machine: 我的机器上安装了多个Visual Studios:

  • Visual Studio 2013 Professional. Visual Studio 2013专业版。
  • Visual Studio 2015 Community Edition. Visual Studio 2015社区版。
  • Visual Studio Express Editions. Visual Studio Express Editions。

I need to detect the path to the Visual Studio from which my VSPackage is executing its command. 我需要检测我的VSPackage执行其命令的Visual Studio路径

How can I get the current running Visual Studio's installation path in the package? 如何在程序包中获取当前运行的Visual Studio的安装路径

First, I agree with Carlos in that particular point that an extension should never require elevated priviledges. 首先,我同意卡洛斯的观点,即延伸不应该要求提升特权。 But that does not mean, your problem cannot be solved; 但这并不意味着,你的问题无法解决; I would just suggest to do it in another way... 我只是建议以另一种方式做到这一点......

I had a similiar issue with one of my extensions; 我的一个扩展程序有一个类似的问题; and I was looking for a solution which would not require a Windows installer setup, but work for pure VSIX packages. 我正在寻找一个不需要Windows安装程序设置的解决方案,但是适用于纯VSIX软件包。 I solved it by creating a small console application, which I referenced by my package assembly. 我通过创建一个小的控制台应用程序来解决它,我的包装配件引用了它。 I added an application manifest to the console application, allowing me to request the required execution level; 我向控制台应用程序添加了一个应用程序清单,允许我请求所需的执行级别; for instance: 例如:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

The console application looks like... 控制台应用程序看起来像......

public class HelperExe
{
    public static int Main(params string[] args)
    {
        // TODO: 
    }
}

The console application will do the work that requires elevated priviledges. 控制台应用程序将执行需要提升权限的工作。 The package creates a new process using the Process class; 该包使用Process类创建一个新进程; the image´s file path can be obtained from the defining assembly (because this might be a random path, if the package is installed from VSIX). 可以从定义程序集中获取映像的文件路径(因为如果从VSIX安装该程序包,这可能是一个随机路径)。

var consoleAssemblyLocation = new Uri(typeof(HelperExe).Assembly.CodeBase);
var file = new FileInfo(consoleAssemblyLocation.LocalPath);
if (file.Exists)
{
    var consoleProcess = new Process 
    {
        StartInfo = new ProcessStartInfo(file.FullName)
        {
            CreateNoWindow = true
        }
    };

    consoleProcess.Start();
    var timeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;
    consoleProces.WaitForExit(timeout);
}

Since the manifest will involve the UAC to elevate the process... this has also the nice side-effect, that the user can cancel that operation. 由于清单将涉及UAC以提升流程......这也具有良好的副作用,即用户可以取消该操作。 Make sure that your extension can handle that... 确保您的扩展程序可以处理...

Visual Studio´s installation folder can be read from the registry; 可以从注册表中读取Visual Studio的安装文件夹; you can pass the obtained path to the console application via a commandline argument. 您可以通过命令行参数将获取的路径传递给控制台应用程序。 I did it like this... 我这样做了......

static string GetVisualStudioInstallationFolder(string visualStudioVersion)
{
    string subKeyName = string.Format(
        CultureInfo.InvariantCulture, 
        @"Software\Microsoft\VisualStudio\{0}_Config", 
        visualStudioVersion);

    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(subKeyName))
    {
        if (key != null)
        {
            return (string)key.GetValue("ShellFolder");
        }
    }

    return null;
}

The visualStudioVersion parameter can be obtained from the DTE.Version property... visualStudioVersion参数可以从DTE.Version属性获得...

Your package will not be able to copy files to the VS installation path because admin rights are required for that and VS doesn't run elevated (with admin rights) by default, and a package should not force VS to run elevated. 您的程序包将无法将文件复制到VS安装路径,因为默认情况下需要管理员权限,VS不会升级(具有管理员权限),并且程序包不应强制VS运行提升。 The setup of your package could do that, but not your package. 您的软件包的设置可以做到这一点,但不是您的软件包。

That said, see: 那说,见:

HOWTO: Get information about the Visual Studio IDE from a Visual Studio package . HOWTO:从Visual Studio包中获取有关Visual Studio IDE的信息

Finally managed to find the installation path with the following code: 最后设法使用以下代码查找安装路径:

   var test = ((EnvDTE.DTE)ServiceProvider.GlobalProvider.GetService(typeof(EnvDTE.DTE).GUID)).FullName;
   VISUAL_STUDIO_INSTALLATION_PATH = Path.GetFullPath(Path.Combine(test, @"..\..\..\"));

Also for getting the Version of the running Visual Studio: 还用于获取正在运行的Visual Studio的版本:

 EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
 VISUAL_STUDIO_VERSION = dte.Version;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从VSPackage获取Visual Studio配色方案 - Get the Visual Studio color scheme from a VSPackage 安装后Visual Studio VSPackage行为不同 - Visual studio VSPackage behavior different after installation Visual Studio 2017-如何从ENVDTE获取当前运行的版本 - Visual Studio 2017 - How to get the current running edition from ENVDTE 如何从VSPackage获取当前解决方案目录? - How do you get the current solution directory from a VSPackage? 如何在VSPackage插件中捕获Visual Studio命令? - How to capture Visual Studio commands in a VSPackage Plugin? 如何在vspackage中获取当前的解决方案配置? - How to get current solution configuration in vspackage? 如何使用vspackage从Visual Studio中的不同扩展名合并两个名称相同的顶级菜单 - How to merge two top level menu with same name from different extension in visual studio using vspackage 如何更改Visual Studio集成包(VSPackage)上的图标? - How do I change icon on a Visual Studio Integration Package (VSPackage)? 如何使用VSPackage检测Visual Studio IDE是否正在关闭? - How to Detect if Visual Studio IDE is closing using VSPackage? 如何在VSPackage中动态添加命令(Visual Studio 2013程序包) - How to add commands dynamically in VSPackage (Visual Studio 2013 Package)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM