Reputation: 155
I tried to get build date in the csproj of a .NET MAUI solution file like I did in a regular WPF Application: 1.0 $([System.DateTime]::Now.ToString())
But I cant compile it , I get an error NETSDK1005 ... project.assets.json I tried it with the current VS2022 17.4.3 and also Preview 17.5.0 2.0
If it don't work with ApplicationDisplayVersion is there any other way to get the build date at runtime ?
Upvotes: 1
Views: 1075
Reputation: 8220
You could use MSBuild property. Besides having only the assembly version we can have the version + time of the build. We could use SourceRevisionId tag to tell .NET compiler do this.
Add these following to yourappname.csproj file
<PropertyGroup>
<SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</SourceRevisionId>
</PropertyGroup>
Then you could retrieve the build date through AssemblyInformationalVersionAttribute using the following code:
private static DateTime GetBuildDate(Assembly assembly)
{
const string BuildVersionMetadataPrefix = "+build";
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (attribute?.InformationalVersion != null)
{
var value = attribute.InformationalVersion;
var index = value.IndexOf(BuildVersionMetadataPrefix);
if (index > 0)
{
value = value.Substring(index + BuildVersionMetadataPrefix.Length);
if (DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
{
return result;
}
}
}
return default;
}
Then you could get build date through the method
DateTime dt = GetBuildDate(Assembly.GetExecutingAssembly());
For more info, you could refer to Getting the date of build of a .NET assembly at runtime.
Hope it works for you.
Upvotes: 3