visual-studiovisual-studio-2017

Programmatically finding the VS2017 installation directory


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

HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0

However, this doesn't seem to work with the VS2017 RC. We have scripts that detect the latest installed VS and then do "the right thing", and so far I'm having issues plugging VS2017 into those systems.

Does anyone know how to programmatically determine the installation location for VS2017?


Solution

  • You can use vswhere tool to get VS2017 location.

    Example:

    @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\15.0\Bin\MSBuild.exe" (
      "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
    )
    

    You can read more about it here: https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/