Programmatically finding the VS2017 installation directory

前端 未结 10 1202
梦谈多话
梦谈多话 2020-12-06 17:49

With previous versions of VS you could query the registry to determine the installation directory for VS:

HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Visu

10条回答
  •  醉梦人生
    2020-12-06 18:19

    KindDragon's solution didn't quite work for me due to batch's "delayed expansion" "feature". (WAT)

    Here is my code, compatible with VS 2017 15.2 (for the vswhere.exe installation)

    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"
    )
    

    Especially note usage of SETLOCAL EnableDelayedExpansion and !InstallDir!

提交回复
热议问题