I currently have an app displaying the build number in its title window. That's well and good except it means nothing to most of the users, who want to know if they have the latest build - they tend to refer to it as "last Thursday's" rather than build 1.0.8.4321.
The plan is to put the build date there instead - So "App built on 21/10/2009" for example.
I'm struggling to find a programmatic way to pull the build date out as a text string for use like this.
For the build number, I used:
Assembly.GetExecutingAssembly().GetName().Version.ToString()
after defining how those came up.
I'd like something like that for the compile date (and time, for bonus points).
Pointers here much appreciated (excuse pun if appropriate), or neater solutions...
For .NET Core projects, I adapted Postlagerkarte's answer to update the assembly Copyright field with the build date.
Directly Edit csproj
The following can be added directly to the first
PropertyGroup
in the csproj:Alternative: Visual Studio Project Properties
Or paste the inner expression directly into the Copyright field in the Package section of the project properties in Visual Studio:
This can be a little confusing, because Visual Studio will evaluate the expression and display the current value in the window, but it will also update the project file appropriately behind the scenes.
Solution-wide via Directory.Build.props
You can plop the
<Copyright>
element above into aDirectory.Build.props
file in your solution root, and have it automatically applied to all projects within the directory, assuming each project does not supply its own Copyright value.Directory.Build.props: Customize your build
Output
The example expression will give you a copyright like this:
Retrieval
You can view the copyright information from the file properties in Windows, or grab it at runtime:
One approach which I'm amazed no-one has mentioned yet is to use T4 Text Templates for code generation.
Pros:
Cons:
The above method can be tweaked for assemblies already loaded within the process by using the file's image in memory (as opposed to re-reading it from storage):
I'm not sure, but maybe the Build Incrementer helps.
Jeff Atwood had a few things to say about this issue in Determining Build Date the hard way.
The most reliable method turns out to be retrieving the linker timestamp from the PE header embedded in the executable file -- some C# code (by Joe Spivey) for that from the comments to Jeff's article:
Usage example:
UPDATE: The method was working for .Net Core 1.0, but stopped working after .Net Core 1.1 release(gives random years in 1900-2020 range)
For anyone that needs to get the compile time in Windows 8 / Windows Phone 8:
For anyone that needs to get the compile time in Windows Phone 7:
NOTE: In all cases you're running in a sandbox, so you'll only be able to get the compile time of assemblies that you deploy with your app. (i.e. this won't work on anything in the GAC).