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...
You could launch an extra step in the build process that writes a date stamp to a file which can then be displayed.
On the projects properties tab look at the build events tab. There is an option to execute a pre or post build command.
I used Abdurrahim's suggestion. However, it seemed to give a weird time format and also added the abbreviation for the day as part of the build date; example: Sun 12/24/2017 13:21:05.43. I only needed just the date so I had to eliminate the rest using substring.
After adding the
echo %date% %time% > "$(ProjectDir)\Resources\BuildDate.txt"
to the pre-build event, I just did the following:The good news here is that it worked.
I am just C# newbie so maybe my answer sound silly - I display the build date from the date the executable file was last written to:
I tried to use File.GetCreationTime method but got weird results: the date from the command was 2012-05-29, but the date from the Window Explorer showed 2012-05-23. After searching for this discrepancy I found that the file was probably created on 2012-05-23 (as shown by Windows Explorer), but copied to the current folder on 2012-05-29 (as shown by File.GetCreationTime command) - so to be on the safe side I am using File.GetLastWriteTime command.
Zalek
I needed a universal solution that worked with a NETStandard project on any platform (iOS, Android, and Windows.) To accomplish this, I decided to automatically generate a CS file via a PowerShell script. Here is the PowerShell script:
Save the PowerScript file as GenBuildDate.ps1 and add it your project. Finally, add the following line to your Pre-Build event:
Make sure BuildDate.cs is included in your project. Works like a champ on any OS!