.net MSBuild 的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/328017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Path to MSBuild
提问by dagda1
How can I programatically get the path to MSBuild from a machine where my .exe is running?
如何以编程方式从运行我的 .exe 的机器获取 MSBuild 的路径?
I can get the .NET version from the Environment but is there a way of getting the correct folder for a .NET version?
我可以从 Environment 获取 .NET 版本,但是有没有办法获取 .NET 版本的正确文件夹?
采纳答案by Brian
Poking around the registry, it looks like
浏览注册表,它看起来像
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.5
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0
may be what you're after; fire up regedit.exe and have a look.
可能是你所追求的;启动 regedit.exe 并查看。
Query via command line (per Nikolay Botev)
通过命令行查询(根据Nikolay Botev)
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0" /v MSBuildToolsPath
Query via PowerShell (per MovGP0)
通过 PowerShell 查询(每个MovGP0)
dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\
回答by Nikolay Botev
You can also print the path of MSBuild.exe to the command line:
您还可以将 MSBuild.exe 的路径打印到命令行:
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0" /v MSBuildToolsPath
回答by AllenSanborn
If you want to use MSBuild for .Net 4 then you can use the following PowerShell command to get the executable's path. If you want version 2.0 or 3.5 then just change the $dotNetVersion variable.
如果要为 .Net 4 使用 MSBuild,则可以使用以下 PowerShell 命令来获取可执行文件的路径。如果您想要 2.0 或 3.5 版,那么只需更改 $dotNetVersion 变量。
To run the executable you'll need to prepend the $msbuild variable with &. That will execute the variable.
要运行可执行文件,您需要在 $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
回答by yoyo
For cmd shell scripting in Windows 7, I use the following fragment in my batch file to find MSBuild.exe in the .NET Framework version 4. I assume version 4 is present, but don't assume the sub-version. This isn't totally general-purpose, but for quick scripts it may be helpful:
对于 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
For my uses I'm exiting the batch file with an error if that didn't work:
对于我的使用,如果不起作用,我将退出批处理文件并显示错误:
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
回答by dhcgn
You can use this very trial PowerShell Command to get the MSBuildToolsPathfrom the registry.
您可以使用这个非常试用的 PowerShell 命令MSBuildToolsPath从注册表中获取。
PowerShell (from registry)
PowerShell(来自注册表)
Resolve-Path HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\* |
Get-ItemProperty -Name MSBuildToolsPath
Output
输出
MSBuildToolsPath : C:\Program Files (x86)\MSBuild.0\bin\amd64\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.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.0\bin\amd64\
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.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.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.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.0
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions
PSChildName : 4.0
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
or from the filesystem
或从文件系统
PowerShell (from file system)
PowerShell(来自文件系统)
Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\amd64\MSBuild.exe"
Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\MSBuild.exe"
Output
输出
Path
----
C:\Program Files (x86)\MSBuild.0\Bin\amd64\MSBuild.exe
C:\Program Files (x86)\MSBuild.0\Bin\amd64\MSBuild.exe
C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe
C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe
回答by Ian Kemp
Instructions for finding MSBuild:
- PowerShell:
&"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe - CMD:
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe
- 电源外壳:
&"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe - 指令:
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe
Instructions for finding VSTest:
- PowerShell:
&"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.PackageGroup.TestTools.Core -find Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe - CMD:
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.PackageGroup.TestTools.Core -find Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
- 电源外壳:
&"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.PackageGroup.TestTools.Core -find Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe - 指令:
"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.PackageGroup.TestTools.Core -find Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
(Note that the instructions above are slightly modified from Microsoft's official ones. In particular, I've included the -prereleaseflag to allow Preview and RC installations to be picked up, and the -products *to detect Visual Studio Build Tools installations.)
(请注意,上面的说明与 Microsoft 的官方说明略有修改。特别是,我包含了-prerelease允许选择预览和 RC 安装的标志,以及-products *检测 Visual Studio Build Tools 安装的标志。)
It only took over two years but finally in 2019, Microsoft has listened and given us a way to find these vital executables! If you have Visual Studio 2017 and/or 2019 installed, the vswhereutility can be queried for the location of MSBuild et al. Since vswhereis always located at%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe, there is no bootstrapping and no path hardcoding required anymore.
仅用了两年多的时间,终于在 2019 年,Microsoft 听取了我们的意见,并为我们提供了一种找到这些重要可执行文件的方法!如果您安装了 Visual Studio 2017 和/或 2019,则vswhere可以查询该实用程序以获取 MSBuild 等的位置。由于vswhere始终位于%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe,因此不再需要引导和路径硬编码。
The magic is the -findparameter, added in version 2.6.2. You can determine the version you have installed by running vswhere, or checking its file properties. If you have an older version, you can simply download the latest oneand overwrite the existing %ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe.
神奇的是在 2.6.2 版本中添加的-find参数。您可以通过运行或检查其文件属性来确定已安装的版本。如果您有旧版本,则只需下载最新版本并覆盖现有的.vswhere%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
vswhere.exeis a standalone executable, so you can download and run it from anywhere you have an internet connection. That means your build scripts can check if the environment they're running on is setup correctly, to name one option.
vswhere.exe是一个独立的可执行文件,因此您可以从任何有 Internet 连接的地方下载并运行它。这意味着您的构建脚本可以检查它们运行的环境是否设置正确,以命名一个选项。
回答by JJS
@AllenSanborn has a great powershell version, but some folks have a requirement to use only batch scripts for builds.
@AllenSanborn 有一个很棒的 powershell 版本,但有些人要求只使用批处理脚本进行构建。
This is an applied version of what @bono8106 answered.
这是@bono8106 回答的应用版本。
msbuildpath.bat
msbuildpath.bat
@echo off
reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions.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.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
编译.bat
@echo off
call msbuildpath.bat
"%MSBUILDDIR%msbuild.exe" foo.csproj /p:Configuration=Release
For Visual Studio 2017 / MSBuild 15, Aziz Atif (the guy who wrote Elmah) wrote a batch script
对于 Visual Studio 2017 / MSBuild 15,Aziz Atif(编写Elmah 的人)编写了一个批处理脚本
build.cmd Release Foo.csproj
https://github.com/linqpadless/LinqPadless/blob/master/build.cmd
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 Studio17\%%e\MSBuild.0\Bin\MSBuild.exe" (
set "MSBUILD=%PROGRAMS%\Microsoft Visual Studio17\%%e\MSBuild.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
回答by Raman Zhylich
This works for Visual Studio 2015 and 2017:
这适用于 Visual Studio 2015 和 2017:
function Get-MSBuild-Path {
$vs14key = "HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions.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.0\bin\amd64\msbuild.exe"
}
}
return $msbuildPath
}
回答by Paulo Santos
The Registry locations
注册表位置
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.5
give the location for the executable.
给出可执行文件的位置。
But if you need the location where to save the Task extensions, it's on
但是如果你需要保存任务扩展的位置,它就在
%ProgramFiles%\MSBuild
回答by MovGP0
easiest way might be to open PowerShell and enter
最简单的方法可能是打开 PowerShell 并输入
dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\

