If using ..NET Core, add the following class to your project as per below.
Note there is no namespace used, keep it as shown
using System;
using System.Globalization;
[AttributeUsage(AttributeTargets.Assembly)]
internal class BuildDateAttribute : Attribute
{
public BuildDateAttribute(string value)
{
DateTime = DateTime.ParseExact(
value,
"yyyyMMddHHmmss",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
}
public DateTime DateTime { get; }
}
Add this to the .csproj file
<ItemGroup>
<AssemblyAttribute Include="BuildDateAttribute">
<_Parameter1>$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
Add the following to the class which gets the build date
private static DateTime GetBuildDate(Assembly assembly)
{
var attribute = assembly.GetCustomAttribute<BuildDateAttribute>();
return attribute?.DateTime ?? default(DateTime);
}
Get the build date-time
DateTime buildDate = GetBuildDate(Assembly.GetExecutingAssembly());