c# Build a project again at post-build

Viewed 23

I have a c# project on visual studio 2019. I want to build the project again at the post-build event command line, I have this written:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe" "$(ProjectDir)ProfileTypes.csproj"

However, this makes it build it infinite times understandably. Do you know how can I make this line to run only the first time post-build was called?

1 Answers

You could check for an environment variable being set, only repeat the build if it does not exist - the environment variable itself will only exist for the duration of the build, so does not need clearing after each build.

So change your post-build event to something like :

if a%RptBuild% NEQ a goto end
set  RptBuild=1
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe" "$(ProjectDir)ProfileTypes.csproj"
:end

First time the post-build event occurs the environment variable does not exist, so a%RptBuild% evaluates to a - so the if statement goto is not performed, the environment variable is created with a dummy value & the build repeated. The second time through a%RptBuild% evaluates to a1 so the goto statement skips over the build & there is no further post-build event.

Related