如何从VSPackage的当前运行Visual Studio安装路径 [英] How get the current running Visual Studio installation path from VSPackage

查看:356
本文介绍了如何从VSPackage的当前运行Visual Studio安装路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 VSPackage的应一些XML架构文件复制到Visual Studio的安装路径为: VS%安装路径%\Xml\\ 。\\Schemas

I've created a VSPackage which should copy some XML schema files to Visual Studio's installation path: %VS install path% \Xml\Schemas.

我有多个视觉工作室安装在我的机器上:

I have multiple Visual Studios installed on my machine:


  • 的Visual Studio 2013专业版。

  • 的Visual Studio 2015社区版。

  • 的Visual Studio Express版。

我需要检测的路径的Visual Studio 从中我的 VSPackage的正在执行其命令。

I need to detect the path to the Visual Studio from which my VSPackage is executing its command.

我怎样才能在当前运行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...

我有一个类似的问题,我的扩展之一;我一直在寻找这不会需要Windows Installer安装程序,但对于纯粹的VSIX包工作的解决方案。我通过创建一个小型控制台应用程序,这是我用我的包组装引用解决它。我添加应用程序清单的控制台应用程序,允许我请求所需的执行水平;例如:

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

控制台应用程序将做到这一点需要提升权限的工作。包创建使用过程类的新进程;可以从限定装配可获得image's文件路径(因为这可能是一个随机的路径,如果该包从VSIX安装)。

The console application will do the work that requires elevated priviledges. The package creates a new process using the Process class; 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).

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



由于该清单将包括 UAC 来提升的过程...这也有漂亮的副作用,用户可以取消操作。请确保您的扩展可以处理...

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. Make sure that your extension can handle that...

视觉Studio's安装文件夹,可以从注册表中读取;您可以通过一个命令行参数传递所获得的路径控制台应用程序。我做了这样的...

Visual Studio´s installation folder can be read from the registry; 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;
}



visualStudioVersion 参数可以从 DTE.Version 属性获得..

The visualStudioVersion parameter can be obtained from the DTE.Version property...

这篇关于如何从VSPackage的当前运行Visual Studio安装路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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