如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
当前回答
为https://github.com/linqpadless/LinqPadless/blob/master/build.cmd添加vwhere分支,在我的计算机上运行良好,vwhere分支在我伴侣的计算机上运行良好。 也许,vswhere分支应该作为第一个检查向前移动。
@echo off
setlocal
if "%PROCESSOR_ARCHITECTURE%"=="x86" set PROGRAMS=%ProgramFiles%
if defined ProgramFiles(x86) set PROGRAMS=%ProgramFiles(x86)%
for %%e in (Community Professional Enterprise) do (
if exist "%PROGRAMS%\Microsoft Visual Studio\2017\%%e\MSBuild\15.0\Bin\MSBuild.exe" (
set "MSBUILD=%PROGRAMS%\Microsoft Visual Studio\2017\%%e\MSBuild\15.0\Bin\MSBuild.exe"
)
)
if exist "%MSBUILD%" goto :build
for /f "usebackq tokens=1* delims=: " %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -requires Microsoft.Component.MSBuild`) do (
if /i "%%i"=="installationPath" set InstallDir=%%j
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
set "MSBUILD=%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe"
)
if exist "%MSBUILD%" goto :build
set MSBUILD=
for %%i in (MSBuild.exe) do set MSBUILD=%%~dpnx$PATH:i
if not defined MSBUILD goto :nomsbuild
set MSBUILD_VERSION_MAJOR=
set MSBUILD_VERSION_MINOR=
for /f "delims=. tokens=1,2,3,4" %%m in ('msbuild /version /nologo') do (
set MSBUILD_VERSION_MAJOR=%%m
set MSBUILD_VERSION_MINOR=%%n
)
echo %MSBUILD_VERSION_MAJOR% %MSBUILD_VERSION_MINOR%
if not defined MSBUILD_VERSION_MAJOR goto :nomsbuild
if not defined MSBUILD_VERSION_MINOR goto :nomsbuild
if %MSBUILD_VERSION_MAJOR% lss 15 goto :nomsbuild
if %MSBUILD_VERSION_MINOR% lss 1 goto :nomsbuild
:restore
for %%i in (NuGet.exe) do set nuget=%%~dpnx$PATH:i
if "%nuget%"=="" (
echo WARNING! NuGet executable not found in PATH so build may fail!
echo For more on NuGet, see https://github.com/nuget/home
)
pushd "%~dp0"
popd
goto :EOF
:build
setlocal
"%MSBUILD%" -restore -maxcpucount %1 /p:Configuration=%2 /v:m %3 %4 %5 %6 %7 %8 %9
goto :EOF
:nomsbuild
echo Microsoft Build version 15.1 (or later) does not appear to be
echo installed on this machine, which is required to build the solution.
exit /b 1
其他回答
为https://github.com/linqpadless/LinqPadless/blob/master/build.cmd添加vwhere分支,在我的计算机上运行良好,vwhere分支在我伴侣的计算机上运行良好。 也许,vswhere分支应该作为第一个检查向前移动。
@echo off
setlocal
if "%PROCESSOR_ARCHITECTURE%"=="x86" set PROGRAMS=%ProgramFiles%
if defined ProgramFiles(x86) set PROGRAMS=%ProgramFiles(x86)%
for %%e in (Community Professional Enterprise) do (
if exist "%PROGRAMS%\Microsoft Visual Studio\2017\%%e\MSBuild\15.0\Bin\MSBuild.exe" (
set "MSBUILD=%PROGRAMS%\Microsoft Visual Studio\2017\%%e\MSBuild\15.0\Bin\MSBuild.exe"
)
)
if exist "%MSBUILD%" goto :build
for /f "usebackq tokens=1* delims=: " %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -requires Microsoft.Component.MSBuild`) do (
if /i "%%i"=="installationPath" set InstallDir=%%j
)
if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
set "MSBUILD=%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe"
)
if exist "%MSBUILD%" goto :build
set MSBUILD=
for %%i in (MSBuild.exe) do set MSBUILD=%%~dpnx$PATH:i
if not defined MSBUILD goto :nomsbuild
set MSBUILD_VERSION_MAJOR=
set MSBUILD_VERSION_MINOR=
for /f "delims=. tokens=1,2,3,4" %%m in ('msbuild /version /nologo') do (
set MSBUILD_VERSION_MAJOR=%%m
set MSBUILD_VERSION_MINOR=%%n
)
echo %MSBUILD_VERSION_MAJOR% %MSBUILD_VERSION_MINOR%
if not defined MSBUILD_VERSION_MAJOR goto :nomsbuild
if not defined MSBUILD_VERSION_MINOR goto :nomsbuild
if %MSBUILD_VERSION_MAJOR% lss 15 goto :nomsbuild
if %MSBUILD_VERSION_MINOR% lss 1 goto :nomsbuild
:restore
for %%i in (NuGet.exe) do set nuget=%%~dpnx$PATH:i
if "%nuget%"=="" (
echo WARNING! NuGet executable not found in PATH so build may fail!
echo For more on NuGet, see https://github.com/nuget/home
)
pushd "%~dp0"
popd
goto :EOF
:build
setlocal
"%MSBUILD%" -restore -maxcpucount %1 /p:Configuration=%2 /v:m %3 %4 %5 %6 %7 %8 %9
goto :EOF
:nomsbuild
echo Microsoft Build version 15.1 (or later) does not appear to be
echo installed on this machine, which is required to build the solution.
exit /b 1
查找MSBuild的说明:
PowerShell: &"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * - required Microsoft. component .MSBuild -find MSBuild\**\Bin\MSBuild.exe CMD: "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -pre - release -products * - required Microsoft. component .MSBuild -find MSBuild\**\Bin\MSBuild.exe
查找VSTest的说明:
PowerShell: &"${env:ProgramFiles(x86)}\Microsoft VisualStudio \Installer\ vvswhere .exe" -latest -pre - release -products * -需要Microsoft. visualstudio . packagegroup . testtools . core -查找Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe CMD: "%ProgramFiles(x86)%\Microsoft VisualStudio \Installer\ vvswhere .exe" -latest -pre - release -products * - required Microsoft. visualstudio . packagegroup . testtools . core -find Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
(请注意,上面的说明与微软的官方说明略有不同。特别地,我已经包含了-pre - release标志来允许预览和RC安装,以及-products *来检测Visual Studio Build Tools安装。)
这只花了两年多的时间,但最终在2019年,微软听取了我们的意见,并为我们提供了一种找到这些重要可执行文件的方法!如果你安装了Visual Studio 2017和/或2019,vwhere实用程序可以查询MSBuild等的位置。由于vswhere被Microsoft保证位于%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe,因此不再需要引导和路径硬编码。
神奇的是-find形参,它是在2.6.2版本中添加的。您可以通过运行vwhere或检查其文件属性来确定已安装的版本。如果您有较旧的版本,您可以下载最新的版本并覆盖现有的%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe。
Vswhere.exe是一个独立的可执行文件,因此您可以从任何有互联网连接的地方下载并运行它。这意味着您的构建脚本可以检查它们所运行的环境是否设置正确(举个例子)。
如果你已经安装了Chocolatey,你也可以使用相关的vwhere包。
如果你有冒险精神,你也可以在https://github.com/Microsoft/msbuild/releases/上从GitHub获得源代码和MsBuild的最新版本
基于@dh_cgn答案的一行代码:
(Resolve-Path ([io.path]::结合($ {env: ProgramFiles (x86)},“微软Visual Studio ', '*', '*', ' MSBuild ', '*' , ' 本,msbuild.exe .Path)))
它选择所有现有的路径,例如路径。C:\Program Files (x86)\Microsoft Visual Studio\*\*\MSBuild\* bin\ MSBuild .exe。
通配符星号是:
年份(2017年) visual studio版(社区、专业、企业) 工具版本(15.0)
请注意,此命令将选择与按字母排序的表达式匹配的第一个路径。要缩小范围,只需将通配符替换为特定的元素。年份或工具版本。
你也可以把MSBuild.exe的路径打印到命令行:
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0" /v MSBuildToolsPath