9

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?

FrenkyB
  • 6,625
  • 14
  • 67
  • 114
  • Hi, any update for this issue? You can try my answer to check if it helps to resolve that issue. It does work in my machine :( – LoLance Oct 10 '19 at 03:05

3 Answers3

8

Steps that work in my machine:

  1. See this, first we need to make sure MSBuild can be recognized by cmd.exe.

  2. 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)

  3. 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>
Community
  • 1
  • 1
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thanks for your sharing, you can consider adding it as answer to share the useful info. Just a reminder :) – LoLance Oct 14 '19 at 05:10
2

With latest update to VS2019 - version 16.3.4 - the error is no longer there.

FrenkyB
  • 6,625
  • 14
  • 67
  • 114
1

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/

YanMax
  • 75
  • 4