简体   繁体   中英

Run pre-build event in Visual Studio C# on every build without always triggering target rebuild

What I need

I have an .exe that may or may not update one of my .cs files in a VS C# project.
I would like to run this .exe before every build (I am attempting with pre-build events), even if the project is up-to-date.

What I know so far:

When changing a project file on disk, the changes my not be taken into account until the next build, since VS caches the files in RAM. The code below is supposed to prevent this, by always reading files from the disk (code should be added to first PropertyGroup in .csproj file)

<PropertyGroup>
    <UseHostCompilerIfAvailable>false</UseHostCompilerIfAvailable>
</PropertyGroup>

In order to trigger pre-build events on every build, you can disable FastUpToDateCheck like so:

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

The problem

When I do DisableFastUpToDateCheck true , the target is rebuild even when it is up to date, even with an empty pre-build event. I would like my pre-build event to always run, potentially change a file and only if the file was change, or if the project was already not-up-to-date, to have the target rebuilt.

Turns out there was no issues, because VS will not actually rebuild the target (.dll) when it is up-to-date, but I've got confused by the compilation outputs:

project -> long/path/to.dll
========== Build: 1 succeeded, 0 failed, 2 up-to-date, 0 skipped ==========

This message always appears, even if the resulting file is not changed (same last modified date).

Another important note is that when building a project that is not the startup project (let's call them myproject and mystartup), the output of myproject will be written to myproject/bin as needed, but it will only be copied to mystartup/bin when you run the startup project (or rebuild).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM