Programmatically find VS2017 installation directory

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

HKLM \ SOFTWARE \ Wow6432Node \ Microsoft \ VisualStudio \ 14.0

However, this does not seem to work with the VS2017 RC. We have scripts that detect the last installed VS and then do the “right thing”, and so far I have had problems connecting VS2017 to these systems.

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

+8
source share
11 answers

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/

+12
source

Visual Studio 2017 does not support the registry, side-by-side installation of all SKUs (Enterprise, Professional, and Community).

MSI installers can request through the APIs described here: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/

Examples here:

+5
source

KindDragon's solution didn’t work for me because of the “slow expansion” batch feature. (WAT)

Here is my code compatible with VS 2017 15.2 (for installing 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" ) 

In particular, pay attention to the use of SETLOCAL EnableDelayedExpansion and! InstallDir!

+1
source

Well, vswhere.exe really does not provide more than the installation path for the version of Visual Studio. Here my .profile Interix snippet file from 2008 does the same with a small update (shell script):

 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:]]*\).*|\1|p' | sed 's|\\|/|g') if [[ -n $_vsinstalldir ]]; then break; fi done; unset vsver 

This listing of Visual Studio installations in favor of the latter in a registry key

 HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7 

Still works for Visual Studio 2017. It would be easy to translate to cmd syntax. Querying the registry is simpler and does not require vswhere.exe in your path, which is favorable for IMO.

Now finding the current instance of Visual C ++ and the SDK is another task .: D

The general conclusion in case you are wondering:

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

I had the devil of time trying to change Srekel's answer to search only VS2017. Note. If you put the for statement below in an if block, it will destroy control characters and will not work.

 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" ) 
+1
source

Can I recommend my get-vs2017-path package, it uses only the built-in Windows tools (and although it was created as an NPM package, it has no dependencies, and the tools folder works autonomously)

0
source

You can request the installation path of Visual Studio from the registry, see the following answer:

fooobar.com/questions/1265275 / ...

You can use batch download and return it to the application, or even easier - use the registry functions to request a key value.

Limitations of this approach are described in the answer.

0
source

See the following answer here:

fooobar.com/questions/1265275 / ...

You can use any command line tool to query the location of Visual Studio, but I also provide a programmatic way to query the location of Visual Studio. The code is based on vswhere source code, but simplified.

0
source

We have only 3 editions of Visual Studio.

  • \ Community
  • \ Professional
  • \ Enterprise

This way we can simplify things a bit.
Here are some tested CMD scripts.

 @SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio\2017 @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 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 see the result and PAUSE @ECHO VS 2017 install path is @ECHO %unquoted% @PAUSE 
0
source

You can use this PowerShell snippet to find the VS2017 installation directory:

 $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 to VC++ compiler "VsDevCmd.bat Path: " + $vs2017.InstallationPath + "\Common7\Tools\VsDevCmd.bat" 
-2
source

I use PowerShell as KindDragon

 $Is64bitOs = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64'; if ($Is64bitOs){ $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0"; } else { $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0"; } $VSInstallDir = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0 -Name ShellFolder).ShellFolder; 
-3
source

Source: https://habr.com/ru/post/1265277/


All Articles