Search code examples
c#.netmsbuildcsproj

How to ignore warnings and errors of a postbuild event?


I have a postbuild event in my csproj. I want to ignore the output from this command, but whenever I do command >nul 2>&1 this csproj goes corrupt, probably because of the ">". I noticed when I write ">" from the postbuild window instead of editing the csproj directly if gets encoded.. Is there a workaround (other than running it from a bat file)


Solution

  • This can be done using MSBuild's <Exec> task from a custom target instead of relying on the default pre/post build script properties.

    You can add this to your csproj file instead of using the default pre/post build scripts:

    <Target Name="PostBuild" AfterTargets="PostBuildEvent">
      <Exec Command="some command" IgnoreExitCode="true" />
    </Target>
    

    Note that for "sdk-based" projects (ASP.NET Core, .NET Core, .NET Standard) the above format is what is added when specifying a post build event in the Visual Studio UI already with the exception of the IgnoreExitCode attribute.