【问题标题】:reliable way to find the location devenv.exe of Visual Studio 2017查找 Visual Studio 2017 的 devenv.exe 位置的可靠方法
【发布时间】:2017-07-31 11:52:02
【问题描述】:

我需要运行使用devenv.exe(或devenv.com)构建视觉工作室解决方案的脚本。对于 Visual Studio 2015,有一个环境变量 %VS140COMNTOOLS%,我可以使用它来查找 devenv 的安装位置。由于there is no %VS150COMNTOOLS% for Visual Studio 2017,在脚本(bat 或 powershell)中找到devenv 的安装位置的可靠方法是什么。

【问题讨论】:

标签: visual-studio-2017 devenv


【解决方案1】:

我找到了一个很简单的方法,只要运行Visual Studio Developer Command Prompt,然后运行这个命令:where devenv

它将返回 devenv.com 和 devenv.exe 文件的完整路径

【讨论】:

  • 只有我在 VS 中可以找到的相关窗口被称为“命令窗口”,其中显示 >where devenv 命令“where”无效。
  • 我认为这取决于VS版本,我使用的是Enterprise 2019及其工具->命令行->开发人员命令提示符
  • 多年来,开发者命令提示符也可从 Windows 开始菜单中使用。
【解决方案2】:

这是一个 PowerShell 函数,可用于获取 DevEnv.exe 路径。您还可以轻松修改它以查找其他常见组件,例如 MsBuild.exeTF.exe,只需更改 -Filter 中使用的硬编码 DevEnv.exe 字符串即可。

function Get-DevEnvExecutableFilePath
{
    [bool] $vsSetupExists = $null -ne (Get-Command Get-VSSetupInstance -ErrorAction SilentlyContinue)
    if (!$vsSetupExists)
    {
        Write-Verbose "Installing the VSSetup module..."
        Install-Module VSSetup -Scope CurrentUser -Force
    }
    [string] $visualStudioInstallationPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.Component.MSBuild).InstallationPath

    $devEnvExecutableFilePath = (Get-ChildItem $visualStudioInstallationPath -Recurse -Filter "DevEnv.exe" | Select-Object -First 1).FullName
    return $devEnvExecutableFilePath
}

它的工作原理是安装 VSSetup 模块(如果它尚不存在),然后查询该模块以获取最新的 Visual Studio 安装路径。从那里,它会在 Visual Studio 安装路径中搜索 DevEnv.exe 文件并返回其完整路径。

【讨论】:

  • 很好的解决方案!您能否就其工作原理添加更多详细信息?
【解决方案3】:

不幸的是,我自己在此处使用注册表的解决方案不适用于 Visual Studio 2019,因为可以从卸载注册表路径查询 vs2019 安装路径,但开始有点困难,因为您可能需要遍历子注册表项.

我也尝试了一点vswhere.exe,但它的使用也有点困难 - 需要重新解析它的输出。

同时,我需要类似于vswhere 的东西,并看了一下它的实现,并在我自己的命令行工具cppexec.exe 下重新编写了它。

用法是这样的:

D:\Prototyping\cppscriptcore>cppexec.exe -location
Visual studio 2019:
location: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise

Visual studio 2017:
location: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2017
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise

D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2019
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise

要使用它,请下载两个文件:

https://github.com/tapika/cppscriptcore/blob/master/cppexec.exe https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel.dll

如果您有兴趣集成到您自己的项目中,请查看以下源代码:

https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.cpp https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.h

我已将vswhere 重新编码为更简单的方式。

【讨论】:

【解决方案4】:

一种方法是使用 power shell 和 vswhere.exe。但是我有点懒得安装新工具和...

我试图找到更简单的解决方案并从注册表中找到它 - 存在注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7,其中列出了所有 Visual Studio 安装。

此链接中提到的限制之一: https://developercommunity.visualstudio.com/content/problem/2813/cant-find-registry-entries-for-visual-studio-2017.html

如果安装了多个 2017 版本,那么似乎最后安装的版本将在此键中包含其路径。

但通常您只安装一个 Visual Studio 用于构建或使用目的。

我还从 64 位机器的角度编写了这个示例,我认为 Wow6432Node 不会在 32 位机器中退出,但实际上 - 现在有多少开发人员使用 32 位机器?

所以如果你对上述限制没问题,这里有一个简单的批处理,可以查询 Visual Studio 安装路径:

test.bat :

@echo off
setlocal 
call:vs%1 2>nul
if "%n%" == "" (
    echo Visual studio is not supported.
    exit /b
)
for /f "tokens=1,2*" %%a in ('reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7" /v "%n%.0" 2^>nul') do set "VSPATH=%%c"
if "%VSPATH%" == "" (
    echo Visual studio %1 is not installed on this machine
    exit /b
)

echo Visual studio %1 path is "%VSPATH%"
endlocal & exit /b

:vs2017
    set /a "n=%n%+1"
:vs2015
    set /a "n=%n%+2"
:vs2013
    set /a "n=%n%+1"
:vs2012
    set /a "n=%n%+1"
:vs2010
    set /a "n=%n%+10"
    exit /b

可以这样执行:

>test 2010
Visual studio 2010 path is "C:\Program Files (x86)\Microsoft Visual Studio 10.0\"

>test 2012
Visual studio 2012 path is "C:\Program Files (x86)\Microsoft Visual Studio 11.0\"

>test 2013
Visual studio 2013 path is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\"

>test 2014
Visual studio is not supported.

>test 2015
Visual studio 2015 path is "C:\Program Files (x86)\Microsoft Visual Studio 14.0\"

>test 2017
Visual studio 2017 path is "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\"

【讨论】:

  • 在 MSFT Windows 10 x86-64 ``reg'' 实用程序具有 /reg:32 命令行选项(我没有检查此操作系统的更旧版本是否存在开关)。因此,您可以添加-reg:32 并从您的脚本指定的注册表项中删除“WOW6432Node”。 但是我仍然不知道-reg:32 选项是否可用于 MSFT Windows 10 x86 上的“reg”:-)
【解决方案5】:

您可以通过use vswhere.exe 或 powershell 找到您的 Visual Studio 实例:

for /r "usebackq tokens=1* delims=: " %%i in (`vswhere.exe -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop`) do (
    if /i "%%i"=="installationPath" set dir=%%j
)

Install-Module VSSetup -Scope CurrentUser
Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64

也可以通过此 api 找到特定工作负载的路径。

https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/

【讨论】:

  • "vswhere" 找不到。猜测它不在路径中,所以我们必须去它所在的位置,但这意味着我们知道 VS 安装在哪里,所以用 vswhere 查找它没有意义
  • 看起来Visual Studio的新目录结构将有一个顶级目录C:\Program Files (x86)\Microsoft Visual Studio,并且在其下将安装各种不同的工具(例如,那里有一个2017目录. 还提供了一个Installer 目录,vswhere 工具就在其中。所以它不是每个版本的。当然,如果你不安装在通常的地方,那也无济于事。但它似乎(最小测试)你可以在任何地方复制 vswhere 并且它仍然有效。它仍然比以前更糟糕并且不适用于旧的 VS 安装。
  • 为了完整起见,这里是使用 powershell 获取实际安装路径的完整命令:$VSInstallPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64 | Select-Object -first 1).InstallationPath
猜你喜欢
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多