Our managers would like a tooltip on an icon that displays the build date of the app. I would like to have a variable that is assigned the correct value at build time so I don't have to remember to change it every time I compile. Is there a way to do this in C#/.Net?
5 Answers
Try following line of code
DateTime buildDate =
new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime;
Relative answer : How to put the build date of application somewhere in the application?

- 1
- 1

- 175,020
- 35
- 237
- 263
To answer the general question: no, there is no way to make the C# compiler do any work at compile time other than simple arithmetic on numerical types and string concatenation. You cannot get it to embed the current date.
For this specific purpose however, you can use the "third way" in Jeff Atwood's blog post: http://www.codinghorror.com/blog/2005/04/determining-build-date-the-hard-way.html

- 59,148
- 53
- 164
- 220
Easiest is to check the file date/time from the .exe or .dll itself:
Assembly asm = Assembly.GetExecutingAssembly();
if( asm != null && !string.IsNullOrEmpty(asm.Location) ) {
FileInfo info = new FileInfo(asm.Location);
DateTime date = info.CreationTime;
}

- 982
- 7
- 10
How do you compile your solution?
If it is a regular compilation with Visual Studio you can set a Pre-Build event to run a script and change a code file that contains the date.

- 17,924
- 4
- 40
- 58
You could make a tool which gets called at build time and inserts the current time as constant in a code file like assemblyinfo.cs.
Another way is to read the file modification time from the executable file.

- 28,510
- 21
- 92
- 151