Reputation: 7207
I am using VS2019 community and I've set pre-build event:
msbuild "$(ProjectPath)" /t:Clean
I am getting error:
'MSBuild' is not recognized as an internal or external command,
operable program or batch file
How is this possible? Isn't this command supposed to be build into visual studio?
I've checked this and tried to set path environment, but it doesn't help.
Does anybody else has the same problem with this command in VS2019?
Upvotes: 9
Views: 32551
Reputation: 1665
On my Windows computer:
Upvotes: 0
Reputation: 146
Using "dotnet" instead of "msbuild" could work if you got that installed.
So, in my case, instead of running "msbuild /t:restore" I figured I can use "dotnet build" and have the same result.
Here's the documentation in case you want to see more equivalent commands. https://learn.microsoft.com/en-us/dotnet/core/tools/
Upvotes: 3
Reputation: 7207
With latest update to VS2019 - version 16.3.4 - the error is no longer there.
Upvotes: 2
Reputation: 28216
Steps that work in my machine:
See this, first we need to make sure MSBuild
can be recognized by cmd.exe
.
If the command can be recognized by cmd.exe
but not build-event from VS, restart the PC can help resolve this issue.
(Something strange is that for my VS still can't recognize it until a restart of the computer)
For VS2019, the correct msbuild path is C:\Program Files (x86)\Microsoft Visual Studio\2019\xxx\MSBuild\Current\Bin
And here's another workaround:
Apart from adding the path of msbuild.exe
into Environment Path
and call it in pre-build
event, you can also consider using MSBuild Task.
Add script below into xx.csproj
:(work for .net framework...)
<Target Name="MyCleanBeforeBuild" BeforeTargets="BeforeBuild">
<MSBuild Projects="$(ProjectPath)" Targets="clean"/>
<!--<Message Text="Custom Clean" Importance="high"/>-->
</Target>
Upvotes: 9