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 :(