blenddd
blenddd

Reputation: 345

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)

Upvotes: 5

Views: 3328

Answers (2)

Christoph
Christoph

Reputation: 3642

I use a different approach: Resetting the errorlevel variable in the batch. As VS checks %errorlevel%, one can simply set it to zero where a statement should be ignored, e.g.

MD "$(SolutionDir)..\Binaries" 1>NUL 2>NUL
SET ERRORLEVEL=0

If the folder already exists, MD sets error level 1. I overwrite that value with 0 and Visual Studio is happy. Watch out: All further errors are ignored as well, as errorlevels are not assigned anymore if the variable is manually assigned (which is fine for most of the cases).

There is also the possibility to ignore the error like this

MD "$(SolutionDir)..\Binaries" 1>NUL 2>NUL
SET ERRORLEVEL=

(delete the error level), which does not suppress further errors, but it has the side effect that the error we wanted to ignore is displayed in the error list of visual studio instead of the one that caused the problem if an error occurs later in code.

Upvotes: 0

Martin Ullrich
Martin Ullrich

Reputation: 100581

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.

Upvotes: 9

Related Questions