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

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

+15
source share
4 answers

You can use vswhere.exe or powershell to find instances of Visual Studio:

 for /r "usebackq tokens=1* delims=: " %%i in ('vswhere.exe -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop') do ( if /i "%%i"=="installationPath" set dir=%%j ) 

and

 Install-Module VSSetup -Scope CurrentUser Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.VisualStudio.Component.VC.Tools.x86.x64 

The path to specific workloads can also be found in this API.

https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/

+9
source

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\" 
+11
source

Here's the PowerShell function that you can use to get the DevEnv.exe path. You can also easily change it to find other common components, such as MsBuild.exe or TF.exe just by changing the DevEnv.exe line used in -Filter .

 function Get-DevEnvExecutableFilePath { [bool] $vsSetupExists = $null -ne (Get-Command Get-VSSetupInstance -ErrorAction SilentlyContinue) if (!$vsSetupExists) { Write-Verbose "Importing the VSSetup module..." Install-Module VSSetup -Scope CurrentUser -Force } [string] $visualStudioInstallationPath = (Get-VSSetupInstance | Select-VSSetupInstance -Latest -Require Microsoft.Component.MSBuild).InstallationPath $devEnvExecutableFilePath = (Get-ChildItem $visualStudioInstallationPath -Recurse -Filter "DevEnv.exe" | Select-Object -First 1).FullName return $devEnvExecutableFilePath } 
0
source

My own solution using the registry here, unfortunately, does not work for Visual Studio 2019, since it is possible to request the vs2019 installation path from the registry removal path, but it becomes a little more complicated, as you may need to go through sections of the additional registry.

I also tried a little vswhere.exe , but its use is also a bit more complicated - you need to repeat its output.

At the same time, I need something similar to vswhere , I looked at its implementation and rewrote it under my own command line tool called cppexec.exe .

Using it like this:

 D:\Prototyping\cppscriptcore>cppexec.exe -location Visual studio 2019: location: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise Visual studio 2017: location: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2017 C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise D:\Prototyping\cppscriptcore>cppexec.exe -location -vs 2019 C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise 

To use it, upload two files:

https://github.com/tapika/cppscriptcore/blob/master/cppexec.exe https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel.dll

If you are interested in integrating into your own project, take a look at the following source codes:

https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.cpp https://github.com/tapika/cppscriptcore/blob/master/SolutionProjectModel/VisualStudioInfo.h

I transcoded vswhere into a little easier.

0
source

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


All Articles