The attribute [AssemblyVersion()] and [AssemblyFileVersion()] both accepts a constant string as parameter.
So I can create a static class with a const string in the current assembly, and can supply it to these attributes as parameters as below
public static class App
{
public const string Version = "1.0.0.1";
}
and update the AssemblyInfo.cs as
[assembly: AssemblyVersion(App.Version)]
[assembly: AssemblyFileVersion(App.Version)]
This is an alternative suggestion then the automation/trick suggested by @Kelly R and @Gravtion above.
--Update 1--
In case you want to go for T4 template as @KellyR suggested the following can help you with the version:
private string Version()
{
DateTime START = new DateTime(2021,2, 14); //StartDate
DateTime NOW = DateTime.Now;
int numMonths = (int)(NOW.Subtract(START).Days / (365.25 / 12));
int Major = numMonths / 12;
int Minor = numMonths % 12;
int Build = (int)DateTime.Now.Day;
string Revision = $"{(int)DateTime.Now.Hour}{DateTime.Now.Minute}";
return $"{Major}.{Minor}.{Build}.{Revision}";
}
The START stores the start date of the project which is used to calculate the number of months passed till now. Then it's dividing the numMonths by 12 to get the number of year passed(considering it as Major version) and keeps the remaining months as Minor version then the current day as Build and hour + Minutes as Revision.
With that every time you build you will get the latest version of your product.
This can also be used in the T4 template as follows:
<#@ output extension=".cs" #>
<#@ template debug="true" language="C#" hostspecific="false" #>
using System.Reflection;
[assembly: AssemblyVersion("<#= NewVersion() #>")]
[assembly: AssemblyFileVersion("<#= NewVersion() #>")]
<#+
private string NewVersion()
{
DateTime START = new DateTime(2021,2, 14); //StartDate
DateTime NOW = DateTime.Now;
int numMonths = (int)(NOW.Subtract(START).Days / (365.25 / 12));
int Major = numMonths / 12;
int Minor = numMonths % 12;
int Build = (int)DateTime.Now.Day;
string Revision = $"{(int)DateTime.Now.Hour}{DateTime.Now.Minute}";
return $"{Major}.{Minor}.{Build}.{Revision}";
}
//Started On = 12-5-2021;
#>
Just remember to comment AssemblyVersion and AssemblyFileVersion in AssemblyInfo.cs when using it as T4 template.
//[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")]