Visual Studio add pre-build event that always runs (C# project)

kaalus picture kaalus · Mar 7, 2015 · Viewed 21.8k times · Source

In my project, I am running an external tool to update some binary files. These files are included in the project as "content".

At the moment the tool is set to run during "pre-build event" in C# project properties. Unfortunately, this event is only executed if the project is out of date, which is not what I need.

I am working around this by always using "rebuild" instead of "build" on my project, but this is tedious and slow.

I need to execute this tool always, irrespective of whether a project is or is not up to date. Actually, even before MSBuild even determines whether the project is up-to-date, because the tool modifies some of the files included in the project, therefore affecting the up-to-date check result.

Is there a proper way to do it?

Answer

kaalus picture kaalus · Mar 14, 2015

Here's the solution. Define this property in your project file:

<PropertyGroup>
    <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup> 

PreBuildStep will then execute every time, regardless of whether the project is or isn't up to date.

It seems that Visual Studio is bypassing normal up-to-date checks of MSBuild and using some sort of custom check that is faster, but has a side effect of breaking customized build targets.