Adding build date/time/count to your C# project
This is pretty much a straight-forward conversion of http://www.dirty-motherfucker.org/blog/2009/04/24/build-count-of-vc-project-for-version-string/
I was quite pissed that there is no easy option to get something as simple as the build date into your C# project.
So here is my pre-build script I use in my C# projects:
@echo off
SETLOCAL
set TARGET_PATH=%~dp0
set IN_FILENAME="%TARGET_PATH%Version.txt"
set OUT_FILENAME="%TARGET_PATH%Version.cs"
set /p BUILD= < %IN_FILENAME%
set BUILD=%BUILD:~22%
if /I "%BUILD%" == "" set BUILD=0
set /a BUILD=%BUILD%+1
echo #define VERSION_BUILD %BUILD% > %IN_FILENAME%
echo using System; > %OUT_FILENAME%
echo namespace Angler { >> %OUT_FILENAME%
echo class Version { >> %OUT_FILENAME%
echo public const int BuildCount = %BUILD%; >> %OUT_FILENAME%
echo public const string BuildDate = "%DATE%"; >> %OUT_FILENAME%
echo public const string BuildTime = "%TIME%"; >> %OUT_FILENAME%
echo } >> %OUT_FILENAME%
echo } >> %OUT_FILENAME%
ENDLOCAL
I went the easy route with this by simply dropping the build count into an additional file.
I wanted to avoid parsing the value out of the .cs file. But you’re welcome to improve on this :P
When you first run it, it will create the VersionInfo.txt file which will contain the initial build count (for you to adjust).
I place the script in my project directory as “Increase build count.cmd” and add this as a pre-build event: “$(ProjectDir)Increase build count.cmd” (INCLUDING the quotes!)
Now you simply add Version.cs to your project and use its members in your program.
