如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
如何以编程方式从正在运行.exe的机器获取到MSBuild的路径?
我可以从环境中获得。net版本,但是是否有一种方法可以获得。net版本的正确文件夹?
当前回答
最简单的方法可能是打开PowerShell并进入
dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\
其他回答
对于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)。
基于@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)
请注意,此命令将选择与按字母排序的表达式匹配的第一个路径。要缩小范围,只需将通配符替换为特定的元素。年份或工具版本。
本处地点
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\2.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\3.5
给出可执行文件的位置。
但如果你需要保存任务扩展的位置,它是打开的
%ProgramFiles%\MSBuild
您可能认为这里没有太多需要添加的内容,但也许现在是时候在所有版本中使用统一的方式来执行此操作了。我将注册表查询方法(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并进入
dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\