如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
翻了翻登记表,看起来
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.5
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0
也许是你想要的;打开regedit.exe,看一看。
通过命令行查询(由Nikolay Botev提供)
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0" /v MSBuildToolsPath
通过PowerShell查询(每个MovGP0)
dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\
本处地点
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.5
给出可执行文件的位置。
但如果你需要保存任务扩展的位置,它是打开的
%ProgramFiles%\MSBuild
如果你想在。net 4中使用MSBuild,那么你可以使用下面的PowerShell命令来获取可执行文件的路径。如果您想要2.0或3.5版本,只需更改$dotNetVersion变量。
要运行可执行文件,您需要在$msbuild变量前面加上&。这将执行变量。
# valid versions are [2.0, 3.5, 4.0]
$dotNetVersion = "4.0"
$regKey = "HKLM:\software\Microsoft\MSBuild\ToolsVersions\$dotNetVersion"
$regProperty = "MSBuildToolsPath"
$msbuildExe = join-path -path (Get-ItemProperty $regKey).$regProperty -childpath "msbuild.exe"
&$msbuildExe
对于Windows 7中的cmd shell脚本,我在批处理文件中使用以下片段来查找. net Framework版本4中的MSBuild.exe。我假设版本4存在,但不假设是子版本。这不是完全通用的,但对于快速脚本来说可能是有帮助的:
set msbuild.exe=
for /D %%D in (%SYSTEMROOT%\Microsoft.NET\Framework\v4*) do set msbuild.exe=%%D\MSBuild.exe
对于我的使用,我退出批处理文件的错误,如果这不起作用:
if not defined msbuild.exe echo error: can't find MSBuild.exe & goto :eof
if not exist "%msbuild.exe%" echo error: %msbuild.exe%: not found & goto :eof
你也可以把MSBuild.exe的路径打印到命令行:
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0" /v MSBuildToolsPath
@AllenSanborn有一个很棒的powershell版本,但有些人要求只使用批处理脚本进行构建。
这是@bono8106回答的应用版本。
msbuildpath.bat
@echo off
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath > nul 2>&1
if ERRORLEVEL 1 goto MissingMSBuildRegistry
for /f "skip=2 tokens=2,*" %%A in ('reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath') do SET "MSBUILDDIR=%%B"
IF NOT EXIST "%MSBUILDDIR%" goto MissingMSBuildToolsPath
IF NOT EXIST "%MSBUILDDIR%msbuild.exe" goto MissingMSBuildExe
exit /b 0
goto:eof
::ERRORS
::---------------------
:MissingMSBuildRegistry
echo Cannot obtain path to MSBuild tools from registry
goto:eof
:MissingMSBuildToolsPath
echo The MSBuild tools path from the registry '%MSBUILDDIR%' does not exist
goto:eof
:MissingMSBuildExe
echo The MSBuild executable could not be found at '%MSBUILDDIR%'
goto:eof
build.bat
@echo off
call msbuildpath.bat
"%MSBUILDDIR%msbuild.exe" foo.csproj /p:Configuration=Release
对于Visual Studio 2017 / MSBuild 15, Aziz Atif(编写Elmah的人)编写了一个批处理脚本
build.cmd Release Foo.csproj
https://github.com/linqpadless/LinqPadless/blob/master/build.cmd
@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 :restore
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
)
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"
nuget restore ^
&& call :build Debug %* ^
&& call :build Release %*
popd
goto :EOF
:build
setlocal
"%MSBUILD%" /p:Configuration=%1 /v:m %2 %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
您可以使用这个非常试用的PowerShell命令从注册表中获取MSBuildToolsPath。
PowerShell(来自注册表)
Resolve-Path HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\* |
Get-ItemProperty -Name MSBuildToolsPath
输出
MSBuildToolsPath : C:\Program Files (x86)\MSBuild\12.0\bin\amd64\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\12.0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 12.0
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
MSBuildToolsPath : C:\Program Files (x86)\MSBuild\14.0\bin\amd64\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 14.0
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
MSBuildToolsPath : C:\Windows\Microsoft.NET\Framework64\v2.0.50727\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 2.0
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
MSBuildToolsPath : C:\Windows\Microsoft.NET\Framework64\v3.5\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.5
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 3.5
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
MSBuildToolsPath : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 4.0
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
或者从文件系统
PowerShell(来自文件系统)
Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\amd64\MSBuild.exe"
Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\MSBuild.exe"
输出
Path
----
C:\Program Files (x86)\MSBuild\12.0\Bin\amd64\MSBuild.exe
C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe
C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe
C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe
在Windows 2003及更高版本上,在cmd中键入以下命令:
cmd> where MSBuild
Sample result: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
如果没有显示,则意味着. net框架没有包含在系统PATH中。MSBuild应该和。net编译器(vbc.exe, csc.exe)一起放在。net安装文件夹中。
这适用于Visual Studio 2015和2017:
function Get-MSBuild-Path {
$vs14key = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0"
$vs15key = "HKLM:\SOFTWARE\wow6432node\Microsoft\VisualStudio\SxS\VS7"
$msbuildPath = ""
if (Test-Path $vs14key) {
$key = Get-ItemProperty $vs14key
$subkey = $key.MSBuildToolsPath
if ($subkey) {
$msbuildPath = Join-Path $subkey "msbuild.exe"
}
}
if (Test-Path $vs15key) {
$key = Get-ItemProperty $vs15key
$subkey = $key."15.0"
if ($subkey) {
$msbuildPath = Join-Path $subkey "MSBuild\15.0\bin\amd64\msbuild.exe"
}
}
return $msbuildPath
}
如果你想编译一个Delphi项目,请在使用msbuild+Delphi2009时查看“ERROR MSB4040 There is no target in the project”
正确答案是:“有一个批处理文件称为rsvars.bat(在RAD Studio文件夹中搜索它)。在调用MSBuild之前调用它,它将设置必要的环境变量。如果编译器的位置与默认位置不同,请确保rsvars.bat中的文件夹是正确的。”
这个bat不仅会将PATH环境变量更新到正确的。net文件夹,并使用正确的MSBuild.exe版本,还会注册其他必要的变量。
从MSBuild 2017 (v15)开始,MSBuild现在安装在每个Visual Studio版本下的文件夹中
以下是在我的机器上找到MSBuild.exe的一些例子:
C:\windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe (v2.0.50727.8745 32-bit)
C:\windows\Microsoft.NET\Framework64\v2.0.50727\MSBuild.exe (v2.0.50727.8745 64-bit)
C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe (v3.5.30729.8763 32-bit)
C:\Windows\Microsoft.NET\Framework64\v3.5\MSBuild.exe (v3.5.30729.8763 64-bit)
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe (v4.7.2053.0 32-bit)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe (v4.7.2053.0 64-bit)
C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe (v12.0.21005.1 32-bit)
C:\Program Files (x86)\MSBuild\12.0\Bin\amd64\MSBuild.exe (v12.0.21005.1 64-bit)
C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe (v14.0.25420.1 32-bit)
C:\Program Files (x86)\MSBuild\14.0\Bin\amd64\MSBuild.exe (v14.0.25420.1 64-bit)
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe (v15.1.1012+g251a9aec17 32-bit)
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\amd64\MSBuild.exe (v15.1.1012+g251a9aec17 64-bit)
C:\Program Files (x86)\Microsoft Visual Studio\2017\{LicenceName}\MSBuild\Bin\MSBuild.exe (v15.1.1012.6693 32-bit)
C:\Program Files (x86)\Microsoft Visual Studio\2017\{LicenceName}\MSBuild\Bin\amd64\MSBuild.exe (v15.1.1012.6693 64-bit)
在没有其他工具的情况下,从注册表中批量检索msbuild 15 (Visual Studio 2017)的路径:
set regKey=HKLM\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7
set regValue=15.0
for /f "skip=2 tokens=3,*" %%A in ('reg.exe query %regKey% /v %regValue% 2^>nul') do (
set vs17path=%%A %%B
)
set msbuild15path = %vs17path%\MSBuild\15.0\Bin\MSBuild.exe
更好的可用工具:
vswhere:定位Visual Studio 2017和更新的安装,参见get msbuild15 path with batch(使用硬编码路径,如上面的代码片段)。 使用Microsoft.VisualStudio.Workload.MSBuildTools安装VisualStudio PowerShell模块
基于@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)
请注意,此命令将选择与按字母排序的表达式匹配的第一个路径。要缩小范围,只需将通配符替换为特定的元素。年份或工具版本。
对于Visual Studio 2017,在不知道确切版本的情况下,你可以在批处理脚本中使用:
FOR /F "tokens=* USEBACKQ" %%F IN (`where /r "%PROGRAMFILES(x86)%\Microsoft Visual
Studio\2017" msbuild.exe ^| findstr /v /i "amd64"`) DO (SET msbuildpath=%%F)
findstr命令将忽略某些msbuild可执行文件(在本例中为amd64)。
查找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/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/Microsoft/msbuild/releases/上从GitHub获得源代码和MsBuild的最新版本
获取最新版本的MsBuild。 最好的方法,对于所有类型的msbuild安装,不同的处理器架构(Power Shell):
function Get-MsBuild-Path
{
$msbuildPathes = $null
$ptrSize = [System.IntPtr]::Size
switch ($ptrSize) {
4 {
$msbuildPathes =
@(Resolve-Path "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\*\*\MSBuild\*\Bin\msbuild.exe" -ErrorAction SilentlyContinue) +
@(Resolve-Path "${Env:ProgramFiles(x86)}\MSBuild\*\Bin\MSBuild.exe" -ErrorAction SilentlyContinue) +
@(Resolve-Path "${Env:windir}\Microsoft.NET\Framework\*\MSBuild.exe" -ErrorAction SilentlyContinue)
}
8 {
$msbuildPathes =
@(Resolve-Path "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\*\*\MSBuild\*\Bin\amd64\msbuild.exe" -ErrorAction SilentlyContinue) +
@(Resolve-Path "${Env:ProgramFiles(x86)}\MSBuild\*\Bin\amd64\MSBuild.exe" -ErrorAction SilentlyContinue) +
@(Resolve-Path "${Env:windir}\Microsoft.NET\Framework64\*\MSBuild.exe" -ErrorAction SilentlyContinue)
}
default {
throw ($msgs.error_unknown_pointersize -f $ptrSize)
}
}
$latestMSBuildPath = $null
$latestVersion = $null
foreach ($msbuildFile in $msbuildPathes)
{
$msbuildPath = $msbuildFile.Path
$versionOutput = & $msbuildPath -version
$fileVersion = (New-Object System.Version($versionOutput[$versionOutput.Length - 1]))
if (!$latestVersion -or $latestVersion -lt $fileVersion)
{
$latestVersion = $fileVersion
$latestMSBuildPath = $msbuildPath
}
}
Write-Host "MSBuild version detected: $latestVersion" -Foreground Yellow
Write-Host "MSBuild path: $latestMSBuildPath" -Foreground Yellow
return $latestMSBuildPath;
}
您可能认为这里没有太多需要添加的内容,但也许现在是时候在所有版本中使用统一的方式来执行此操作了。我将注册表查询方法(VS2015及以下)与vwhere (VS2017及以上)的使用结合起来,得出了这样的结果:
function Find-MsBuild {
Write-Host "Using VSWhere to find msbuild..."
$path = & $vswhere -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe | select-object -first 1
if (!$path) {
Write-Host "No results from VSWhere, using registry key query to find msbuild (note this will find pre-VS2017 versions)..."
$path = Resolve-Path HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\* |
Get-ItemProperty -Name MSBuildToolsPath |
sort -Property @{ Expression={ [double]::Parse($_.PSChildName) }; Descending=$true } |
select -exp MSBuildToolsPath -First 1 |
Join-Path -ChildPath "msbuild.exe"
}
if (!$path) {
throw "Unable to find path to msbuild.exe"
}
if (!(Test-Path $path)) {
throw "Found path to msbuild as $path, but file does not exist there"
}
Write-Host "Using MSBuild at $path..."
return $path
}
有很多正确答案。然而,这里是我在PowerShell中使用的一行程序来确定最新版本的MSBuild路径:
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\' |
Get-ItemProperty -Name MSBuildToolsPath |
Sort-Object PSChildName |
Select-Object -ExpandProperty MSBuildToolsPath -first 1
这个powershell方法从多个源获取msBuild的路径。按顺序尝试:
首先使用vwhere(因为Visual Studio似乎有更多最新版本的msBuild)。 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe 如果没有发现尝试注册表(框架版本)。 C:\Windows\ Microsoft.NET \ Framework64 \ v4.0.30319 \ msbuild.exe
Powershell代码:
Function GetMsBuildPath {
Function GetMsBuildPathFromVswhere {
# Based on https://github.com/microsoft/vswhere/wiki/Find-MSBuild/62adac8eb22431fa91d94e03503d76d48a74939c
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$path = & $vswhere -latest -prerelease -products * -requires Microsoft.Component.MSBuild -property installationPath
if ($path) {
$tool = join-path $path 'MSBuild\Current\Bin\MSBuild.exe'
if (test-path $tool) {
return $tool
}
$tool = join-path $path 'MSBuild\15.0\Bin\MSBuild.exe'
if (test-path $tool) {
return $tool
}
}
}
Function GetMsBuildPathFromRegistry {
# Based on Martin Brandl's answer: https://stackoverflow.com/a/57214958/146513
$msBuildDir = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\' |
Get-ItemProperty -Name MSBuildToolsPath |
Sort-Object PSChildName |
Select-Object -ExpandProperty MSBuildToolsPath -last 1
$msBuildPath = join-path $msBuildDir 'msbuild.exe'
if (test-path $msBuildPath) {
return $msBuildPath
}
}
$msBuildPath = GetMsBuildPathFromVswhere
if (-Not $msBuildPath) {
$msBuildPath = GetMsBuildPathFromRegistry
}
return $msBuildPath
}