1

Possible Duplicate:
How can I display the Build number and/or DateTime of last build in my app?

I want to append the last DateTime of when my app was built to the titlebar. But this code:

DateTime lastWriteTime = File.GetLastWriteTime(Assembly.GetExecutingAssembly().GetName().ToString());
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime);

...claims that was over 400 years ago (in the year 1600); I find that unlikely.

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Didn't you just ask this question? http://stackoverflow.com/questions/11532705/how-can-i-display-the-build-number-and-or-datetime-of-last-build-in-my-app – zeroef Jul 18 '12 at 16:17
  • @zeroef: No, but a similar one; I gave up on including version info, as that is just something like "1.0.0.0" and a build number is not available. So I am now settling for a "last built time" value, which is working. – B. Clay Shannon-B. Crow Raven Jul 18 '12 at 18:04

3 Answers3

2

The problem is that you've called GetLastWriteTime using something which isn't a filename. Print out Assembly.GetExecutingAssembly().GetName().ToString() and you'll see what I mean.

The documentation for File.GetLastWriteTime calls this out specifically:

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

So, either use Application.ExecutablePath as per Clay's suggestion, or for a specific assembly (or to avoid a WinForms dependency), you could use Application.ManifestModule and get the FullyQualifiedName, like this:

using System;
using System.Reflection;

class Test
{
    static void Main()
    {
        string file = typeof(Test).Assembly
                                  .ManifestModule
                                  .FullyQualifiedName;
        Console.WriteLine(file);
        DateTime lastWriteTime = File.GetLastWriteTime(file);
        Console.WriteLine(lastWriteTime);
    }
}

Of course, that only gets the last write time of the module containing the assembly manifest - it's possible to have multi-module assemblies. It's rare though, and my guess is that this will do you fine.

It's a shame that there isn't a concept of a build time embedded within Assembly itself, but such is life :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

This works:

FileInfo fileInfo = new FileInfo(Application.ExecutablePath);
DateTime lastWriteTime = fileInfo.LastWriteTime;
this.Text = String.Format("Platypi R Us; Built on {0}", lastWriteTime); 
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
1

'GetName()' returns the display name of the assembly. You want Assembly.Location

Also, I'm not sure this will work. If you copy the file to a different location then the file time is out-of-sync with the actual last build time. Maybe manually update it every time you put out a new release?

Kristian Fenn
  • 867
  • 5
  • 14