Chemin vers MSBuild

Comment puis-je programmer le chemin vers MSBuild à partir d'une machine où my .exe est en cours d'exécution?

je peux obtenir la version .NET de L'environnement mais y a-t-il un moyen d'obtenir le bon dossier pour une version .NET?

159
demandé sur Lukasz Lysik 2008-11-30 00:12:51

15 réponses

fouinant autour du registre, il ressemble à

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.5
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0

est peut-être ce que vous cherchez; Lancez regedit.exe et un coup d'oeil.

117
répondu Brian 2012-07-26 08:06:18

vous pouvez aussi imprimer le chemin de MSBuild.exe à la ligne de commande:

reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0" /v MSBuildToolsPath
133
répondu Nikolay Botev 2013-06-19 15:41:06

si vous voulez utiliser MSBuild pour .Net 4, Vous pouvez utiliser la commande PowerShell suivante pour obtenir le chemin de l'exécutable. Si vous voulez la version 2.0 ou 3.5, changez simplement la variable $dotNetVersion.

pour exécuter l'exécutable, vous devez préparer la variable $msbuild avec &. Qui exécutera la variable.

# 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
31
répondu AllenSanborn 2012-11-20 17:32:10

pour cmd shell scripting dans Windows 7, j'utilise le fragment suivant dans mon fichier batch pour trouver MSBuild.exe in the .NET Framework version 4. Je suppose que la version 4 est présente, mais ne supposez pas la sous-version. Ce n'est pas totalement universel-but, mais pour les scripts rapides, il peut être utile:

set msbuild.exe=
for /D %%D in (%SYSTEMROOT%\Microsoft.NET\Framework\v4*) do set msbuild.exe=%%D\MSBuild.exe

pour mon usage je quitte le fichier batch avec une erreur si cela ne fonctionne pas:

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
31
répondu yoyo 2016-05-17 16:55:01

vous pouvez utiliser cette commande de PowerShell pour obtenir le MSBuildToolsPath à partir du registre.

PowerShell (à partir de la base de registre)

Resolve-Path HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\* | 
Get-ItemProperty -Name MSBuildToolsPath

sortie

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

ou du système de fichiers

PowerShell (à partir du système de fichiers)

Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\amd64\MSBuild.exe"
Resolve-Path "C:\Program Files (x86)\MSBuild\*\Bin\MSBuild.exe"

sortie

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
17
répondu dh_cgn 2018-01-25 09:50:58

@AllenSanborn a une grande version powershell, mais certains ont une exigence d'utiliser seulement des scripts de fournée pour les constructions.

c'est une version appliquée de ce que @bono8106 a répondu.

MSBuild Path.MTD

@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
"151940920 de construire".MTD

@echo off
call msbuildpath.bat
"%MSBUILDDIR%msbuild.exe" foo.csproj /p:Configuration=Release

Pour Visual Studio 2017 / MSBuild 15, Aziz Atif (le gars qui a écrit Elmah ) a écrit un script batch

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 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
17
répondu JJS 2018-05-07 17:59:22

Les emplacements de Registre

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.5

indique l'emplacement de l'exécutable.

mais si vous avez besoin de l'emplacement où sauvegarder les extensions de tâche, c'est sur

%ProgramFiles%\MSBuild
6
répondu Paulo Santos 2008-12-08 19:08:19

cela fonctionne pour Visual Studio 2015 et 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

}
5
répondu Raman Zhylich 2017-04-26 00:50:59

la façon la plus facile pourrait être d'ouvrir PowerShell et entrer

dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\
4
répondu MovGP0 2015-07-14 10:33:40

sur Windows 2003 et plus tard, tapez cette commande dans cmd:

cmd> where MSBuild
Sample result: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe

si rien n'apparaît, cela signifie que .net framework n'est pas inclus dans le chemin du système. Le MSBuild devrait être dans le dossier d'installation de .NET, avec les compilateurs de .NET (vbc.exe, le scc.exe) 151920920"

4
répondu draganicimw 2015-09-09 13:20:35

à partir de MSBuild 2017 (v15), MSBuild est maintenant installé dans un dossier sous chaque version de Visual Studio, et n'est plus défini dans le registre

voici quelques exemples de L'endroit où MSBuild.exe se trouve sur ma machine:

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.0\Bin\MSBuild.exe  (v12.0.21005.1 32-bit)
C:\Program Files (x86)\MSBuild.0\Bin\amd64\MSBuild.exe (v12.0.21005.1 64-bit)
C:\Program Files (x86)\MSBuild.0\Bin\MSBuild.exe  (v14.0.25420.1 32-bit)
C:\Program Files (x86)\MSBuild.0\Bin\amd64\MSBuild.exe  (v14.0.25420.1 64-bit)
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\MSBuild.exe  (v15.1.1012+g251a9aec17 32-bit)
C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\amd64\MSBuild.exe (v15.1.1012+g251a9aec17 64-bit)
C:\Program Files (x86)\Microsoft Visual Studio17\{LicenceName}\MSBuild\Bin\MSBuild.exe (v15.1.1012.6693 32-bit)
C:\Program Files (x86)\Microsoft Visual Studio17\{LicenceName}\MSBuild\Bin\amd64\MSBuild.exe (v15.1.1012.6693 64-bit)
3
répondu cowlinator 2017-08-18 20:01:56

pour récupérer le chemin de msbuild 15 (Visual Studio 2017) avec lot à partir du registre sans outils supplémentaires:

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.0\Bin\MSBuild.exe

Mieux les outils disponibles:

2
répondu Roi Danton 2017-12-22 14:10:51

vous pouvez obtenir la version .NET et la version spécifique:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions

C'est de l'emplacement de registre

1
répondu Denis Bubnov 2015-09-03 08:32:07

avec Visual Studio 2017, Microsoft a changé de nouveau L'emplacement de MSBuild. Cette fois-ci, il a été si difficile de le localiser que cela a créé un outil ad hoc pour cette tâche:

Localisateur Visuel De Studio

semble que ce sera la "bonne" façon de découvrir MSBuild, au moins pour VS2017

1
répondu Gian Marco Gherardi 2018-02-04 16:42:36

si vous voulez compiler un projet Delphi, regardez "erreur MSB4040 il n'y a pas de cible dans le projet" en utilisant msbuild+Delphi2009

réponse correcte il y a dit: "il y a un fichier de lot appelé rsvars.bat (rechercher dans le dossier RAD Studio). Appel avant d'appeler MSBuild, et il va configurer les variables d'environnement nécessaires. Assurez-vous que les dossiers sont corrects dans rsvars.chauve-souris si vous avez le compilateur dans un emplacement différent de la valeur par défaut."

cette bat ne mettra pas seulement à jour la variable D'environnement PATH vers le bon dossier .NET avec le bon MSBuild.version exe, mais enregistre également d'autres variables nécessaires.

0
répondu Nashev 2017-06-15 16:18:07