One way is to use Power Shell and vswhere.exe. But I'm a little lazy to install new tools and ...
I tried to find a simpler solution and found it in the registry - there is a registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7 , which lists all the installations of Visual Studio.
One of the limitations mentioned in this link: https://developercommunity.visualstudio.com/content/problem/2813/cant-find-registry-entries-for-visual-studio-2017.html
If more than one 2017 release is installed, then it looks like the last installed path is indicated in this key.
But usually you only install one visual studio for assembly or use.
I also coded this example from the point of view of 64-bit machines, I think that Wow6432Node does not work on 32-bit machines, but really - how many developers currently use 32-bit machines?
So, if you agree with the above limitations, here is a simple package that can request the installation path of 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
It can be done like this:
>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\"
source share