NetLah.Abstractions is .NET library of reusable classes for retrieving .NET assembly build time ApplicationInfo
, AssemblyInfo
.
NetLah.AssemblyInfo.BuildTime.Target is MSBuild target for assembly build-time generation.
This solution come from the idea of Gérald Barré on Meziantou's blog Getting the date of build of a .NET assembly at runtime
<ItemGroup>
<PackageReference Include="NetLah.Abstractions" Version="1.0.0" />
<PackageReference Include="NetLah.AssemblyInfo.BuildTime.Target" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
SampleApp -> C:\Work\NetLah\abstractions\samples\SampleApp\bin\Debug\net6.0\SampleApp.dll
SampleApp [buildTime] := 2021-11-08T21:26:28 (net6.0)
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.84
var asm = new AssemblyInfo(typeof(AssemblyInfo).Assembly);
Console.WriteLine($"AssemblyTitle:{asm.Title}");
Console.WriteLine($"Version:{asm.InformationalVersion} BuildTime:{asm.BuildTimestampLocal}; Framework:{asm.FrameworkName}");
The output:
AssemblyTitle:NetLah.Abstractions
Version:0.2.0-rc2.2 BuildTime:2021-11-08T21:26:28+08:00; Framework:.NETStandard,Version=v2.0
var appInfo = ApplicationInfo.Initialize(null);
Console.WriteLine($"AppTitle:{appInfo.Title}");
Console.WriteLine($"Version:{appInfo.InformationalVersion} BuildTime:{appInfo.BuildTimestampLocal};Framework:{appInfo.FrameworkName}");
Output:
AppTitle:SampleApp
Version:0.2.0-rc2.2 BuildTime:2021-11-08T21:26:56+08:00; Framework:.NETCoreApp,Version=v6.0
4. Use Directory.Build.targets
to declare build date time attribute to all projects inside the solution
- Set build date time to projects that already include packageReference
NetLah.AssemblyInfo.BuildTime.Target
<ItemGroup>
<PackageReference Update="NetLah.Abstractions" Version="1.0.0" />
<PackageReference Update="NetLah.AssemblyInfo.BuildTime.Target" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
- Set build date time to all projects
<ItemGroup>
<PackageReference Update="NetLah.Abstractions" Version="1.0.0" />
<PackageReference Include="NetLah.AssemblyInfo.BuildTime.Target" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>