Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

reliable way to find the location devenv.exe of Visual Studio 2017

I need to run scripts that build a visual studio solutions using devenv.exe (or devenv.com for that matter). For visual studio 2015 there was an environment variable %VS140COMNTOOLS% that I could use to find the install location of devenv. Since there is no %VS150COMNTOOLS% for Visual Studio 2017, what would be a reliable way to find the install location of devenv in a script (bat or powershell).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way is to use power shell and vswhere.exe. But I'm bit lazy to install new tools and ...

I was trying to find simpler solution and found it from registry - there exists registry key HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftVisualStudioSxSVS7, which lists all Visual studio installations.

One of limitations mentioned in this link: https://developercommunity.visualstudio.com/content/problem/2813/cant-find-registry-entries-for-visual-studio-2017.html

If there is more than one edition of 2017 installed, then it seems the last one installed will have their path in this key.

But typically you install only one visual studio for build or use purpose.

Also I've coded this sample from 64-bit machine perspective, I think Wow6432Node does not exits in 32-bit machines, but really - how many developers use 32-bit machines nowadays ?

So if you're fine with limitations above, here is a simple batch which can query visual studio installation path:

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 "HKLMSOFTWAREWow6432NodeMicrosoftVisualStudioSxSVS7" /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

Can be executed 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 Studio2017Professional"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...