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...
The new way
I changed my mind about this, and currently use this trick to get the correct build date.
The old way
Well, how do you generate build numbers? Visual Studio (or the C# compiler) actually provides automatic build and revision numbers if you change the AssemblyVersion attribute to e.g.
1.0.*
What will happen is that is that the build will be equal to the number of days since January 1, 2000 local time, and for revision to be equal to the number of seconds since midnight local time, divided by 2.
see Community Content, Automatic Build and Revision numbers
e.g. AssemblyInfo.cs
SampleCode.cs
You could use a project post-build event to write a text file to your target directory with the current datetime. You could then read the value at run-time. It's a little hacky, but it should work.
You can use this project: https://github.com/dwcullop/BuildInfo
It leverages T4 to automate the build date timestamp. There are several versions (different branches) including one that gives you the Git Hash of the currently checked out branch, if you're into that sort of thing.
Disclosure: I wrote the module.
Lots of great answers here but I feel like I can add my own because of simplicity, performance (comparing to resource-related solutions) cross platform (works with Net Core too) and avoidance of any 3rd party tool. Just add this msbuild target to the csproj.
and now you have
Builtin.CompileTime
ornew DateTime(Builtin.CompileTime, DateTimeKind.Utc)
if you need it that way.ReSharper is not gonna like it. You can ignore him or add a partial class to the project too but it works anyway.
A different, PCL-friendly approach would be to use an MSBuild inline task to substitute the build time into a string that is returned by a property on the app. We are using this approach successfully in an app that has Xamarin.Forms, Xamarin.Android, and Xamarin.iOS projects.
EDIT:
Simplified by moving all of the logic into the
SetBuildDate.targets
file, and usingRegex
instead of simple string replace so that the file can be modified by each build without a "reset".The MSBuild inline task definition (saved in a SetBuildDate.targets file local to the Xamarin.Forms project for this example):
Invoking the above inline task in the Xamarin.Forms csproj file in target BeforeBuild:
The
FilePath
property is set to aBuildMetadata.cs
file in the Xamarin.Forms project that contains a simple class with a string propertyBuildDate
, into which the build time will be substituted:Add this file
BuildMetadata.cs
to project. It will be modified by every build, but in a manner that allows repeated builds (repeated replacements), so you may include or omit it in source control as desired.Add below to pre-build event command line:
Add this file as resource, now you have 'BuildDate' string in your resources.
After inserting the file into the Resource (as public text file), I accessed it via
To create resources, see How to create and use resources in .NET.