0

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?

Anthony
  • 12,177
  • 9
  • 69
  • 105

5 Answers5

3

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?

Community
  • 1
  • 1
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

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

EMP
  • 59,148
  • 53
  • 164
  • 220
1

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;
}
PjL
  • 982
  • 7
  • 10
0

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.

Itay Karo
  • 17,924
  • 4
  • 40
  • 58
0

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.

codymanix
  • 28,510
  • 21
  • 92
  • 151