以编程方式找到VS2017安装目录

Programmatically finding the VS2017 installation directory

对于以前版本的 VS,您可以查询注册表以确定 VS 的安装目录:

HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio.0

但是,这似乎不适用于 VS2017 RC。我们有检测最新安装的 VS 然后执行 "the right thing" 的脚本,到目前为止我在将 VS2017 插入这些系统时遇到问题。

有谁知道如何以编程方式确定 VS2017 的安装位置?

Visual Studio 2017 支持所有 SKU(企业版、专业版和社区版)的免注册、并行安装。

MSI 安装程序可以通过此处描述的 API 进行查询: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/

例子在这里:

您可以使用此 PowerShell 片段查找 VS2017 安装目录:

$vssetup_path = "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\Modules\VSSetup"
if (-not (Test-Path $vssetup_path -pathType container))
{
    iwr https://github.com/Microsoft/vssetup.powershell/releases/download/1.0.36-rc/VSSetup.zip -OutFile "$env:TEMP\VSSetup.zip"
    Expand-Archive "$env:TEMP\VSSetup.zip" $vssetup_path
}
$vs2017 = Get-VSSetupInstance -All | Select-VSSetupInstance -Require 'Microsoft.VisualStudio.Workload.NativeDesktop' -Version '[15.0,16.0)' -Latest
"Installation Path: " + $vs2017.InstallationPath
#`vsdevcmd.bat -arch=x86` or `vsdevcmd.bat -arch=amd64` can be used to setup path's to VC++ compiler
"VsDevCmd.bat Path: " + $vs2017.InstallationPath + "\Common7\Tools\VsDevCmd.bat"

我可以推荐我的包 get-vs2017-path 它只使用 built-in Windows 工具(虽然它是作为一个 NPM 包构建的,但它没有依赖项,并且tools 文件夹独立工作)

我像KindDragon建议的那样使用powershell

$Is64bitOs = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64';
if ($Is64bitOs){
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio.0";
}
else {
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio.0";
}

$VSInstallDir = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio.0 -Name ShellFolder).ShellFolder;

您可以使用 vswhere 工具获取 VS2017 位置。

示例:

@echo off

rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"

for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "%InstallDir%\MSBuild.0\Bin\MSBuild.exe" (
  "%InstallDir%\MSBuild.0\Bin\MSBuild.exe" %*
)

您可以在此处阅读更多相关信息:https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/

由于批处理 "delayed expansion" "feature",KindDragon 的解决方案对我来说不太适用。 (瓦特)

这是我的代码,兼容 VS 2017 15.2(用于 vswhere.exe 安装)

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat" (
  call "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
) else (
  echo "Could not find !InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
)

特别注意 SETLOCAL EnableDelayedExpansion 和 !InstallDir 的用法!

嗯,vswhere.exe 并没有真正提供比 Visual Studio 版本更多的安装路径。这是我 2008 年的 .profile 文件 Interix 片段,做了同样的小更新(shell 脚本):

if [[ -n $PROCESSOR_ARCHITEW6432 || $PROCESSOR_ARCHITECTURE != "x86" ]]; then
  hkeybase='HKLM\SOFTWARE\Wow6432Node\Microsoft\'
else
  hkeybase='HKLM\SOFTWARE\Microsoft\'
fi
for vsver in "15.0" "14.0" "12.0" "11.0" "10.0" "9.0" "8.0"; do
  _vsinstalldir=$(reg.exe query ${hkeybase}'VisualStudio\SxS\VS7' -v $vsver 2>/dev/null \
   | sed -n 's|.*REG_SZ *\([ [:print:]]*\).*||p' | sed 's|\|/|g')
if [[ -n $_vsinstalldir ]]; then break; fi
done; unset vsver

这是在枚举 Visual Studio 支持最新注册表项的安装

HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7

2017 年 Visual Studio 仍在工作。将很容易转换为 cmd 语法。查询注册表更简单,并且不需要 vswhere.exe 在您的路径中,因此对 IMO 有利。

现在查找当前的 Visual C++ 实例和 SDK 完全是另一项任务。 :D

如果您想知道的常见输出:

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/

我花了很多时间试图修改 Srekel 的答案以仅搜索 VS2017。注意:如果您将下面的 "for" 语句放在 "if" 块中,它将破坏转义字符并且不会起作用。

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

set vswherestr=^"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe^" -version [15.0,16.0^^) -products * -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`!vswherestr!`) do (  
  set BUILDVCTOOLS=%%i\Common7\Tools
  echo BUILDVCTOOLS: !BUILDVCTOOLS!
  if not exist !BUILDVCTOOLS!\VsDevCmd.bat (
    echo Error: Cannot find VS2017 Build Tools
    goto :buildfailed
  )
  call "!BUILDVCTOOLS!\VsDevCmd.bat"
)    

在此处查看以下答案:

您可以使用任一命令行工具来查询 Visual studio 位置,但我也提供编程方式来查询 Visual Studio 位置。代码基于 vswhere 源代码,但进行了简化。

我们只有 3 Visual Studio 个版本。

  • \社区
  • \专业
  • \企业

因此我们可以稍微简化一下。
这里有一些经过测试的 CMD 脚本。

@SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio17
@SET env_vs2017_path="%env_all_vs2017_root%\Professional"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Community"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Enterprise"
@REM Let's fail laudly
@IF NOT EXIST %env_vs2017_path%  SET "env_vs2017_path=Visual Studio 2017 install path was not found by %~nx0"

@REM  You may want to remove quotes
@SET unquoted=%env_vs2017_path:"=%


@REM  And now let's see the result and PAUSE

@ECHO VS 2017 install path is
@ECHO %unquoted%

@PAUSE