'How to Change AssemblyInfo.cs AssemblyVersion with date/time and increment revision daily by one in Visual Studio 2022
After dig and search. I find that AssemblyVersion only accept System.Int16 That mean it can't exceed 65,535 value.
the output *.DLL assembly version style I need (YYYY.M.D.HHMMSS) (2022.02.20.060920)
Actually the second part first zero will omit 060920 It will be 60920, What if hour is 15 = 3 PM, It should be 150920 = 6 digits. It will not work. I guess there's a way DateTime.Now.Ticks but it gave a System.Int64 and will never works for Version.
So please is there any 5 digit hour/minute/second style that maybe can used here? Or maybe special 5 digit Tick that have meaning.
The main problem that when I use Visual Studio auto-increment Revision 1.0.0.* feature. It gives arbitrary increment value. And the package output are not ordered.
What should I do now? For now I need to use Time for revision field... Can you help on that please?
Edit, If 5 digit style not avaiable for above. I found article increment revision by one. But its very very old and use JavaScript! https://www.codeproject.com/Tips/161923/Incrementing-AssemblyVersion-revision-number-on-ea
There's also question not answered which still new I would like to increment the build revision version for every build in visual studio
I got following code which get Version 2022.02.20.Revision (Revision are useless during its restricted to large value midnight * 2 from MSDN.
Edit: During the only extension in VS marketplace that do that is not updated to Visual Studio 2022 or maybe they will not supported it, I try to give solution below:
(Feel free to modify Answer if you have better approach)
Solution 1:[1]
I was wish they update extension in VS marketplace.
But anyway here's solution for generate a Version with (Year, Month, Day, Incremental Daily)
This code must inserted before close of </project> tag in *.csproj file
<!-- Change AssemblyInfo.cs AssemblyVersion with date/time and increment revision daily by one in Visual Studio 2022 -->
<Target Name="AssemblyVersion" BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild">
<PropertyGroup>
<!-- Define Constants -->
<AssemblyInfo>Properties\AssemblyInfo.cs</AssemblyInfo>
<AssemblyInfoContent>$([System.IO.File]::ReadAllText($(AssemblyInfo)))</AssemblyInfoContent>
<VersionRegex>(\[\s*assembly\s*:\s*AssemblyVersion\(\s*"(\d+)\.(\d+)\.(\d+)(\.)(\d+)("\)\s*\]))</VersionRegex>
<BuildAndRevisionRegex>(\d+\.\d+")</BuildAndRevisionRegex>
<!-- Parse Build and Revision from AssemblyInfo-->
<AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match('$(AssemblyInfoContent)', '$(VersionRegex)'))</AssemblyVersion>
<BuildAndRevision>$([System.Text.RegularExpressions.Regex]::Match('$(AssemblyVersion)', '$(BuildAndRevisionRegex)'))</BuildAndRevision>
<BuildAndRevision>$(BuildAndRevision.Remove($(BuildAndRevision.LastIndexOf('"')), 1))</BuildAndRevision>
<!-- Generate Build and Revision from AssemblyVersion -->
<Build>$(BuildAndRevision.SubString(0, $(BuildAndRevision.LastIndexOf('.'))))</Build>
<Revision>$(BuildAndRevision.SubString($([MSBuild]::Add($(BuildAndRevision.LastIndexOf('.')), 1))))</Revision>
<!-- Increment Revision by one if Build equal Current Day otherwise start from one as new Day Build-->
<Revision Condition ="$([System.DateTime]::Now.Day) == $(Build)">$([MSBuild]::Add($(Revision), 1))</Revision>
<Revision Condition ="$([System.DateTime]::Now.Day) != $(Build)">1</Revision>
<!-- New AssemblyVersion Block -->
<AssemblyVersion>[assembly: AssemblyVersion("$([System.DateTime]::Now.ToString("yyyy.M.d.$(Revision)"))")]</AssemblyVersion>
</PropertyGroup>
<!-- Write New AssemblyVersion Block to AssemblyInfo.cs file -->
<WriteLinesToFile File="$(AssemblyInfo)" Lines="$([System.Text.RegularExpressions.Regex]::Replace($(AssemblyInfoContent), $(VersionRegex), $(AssemblyVersion)))" Overwrite="true" />
</Target>
A generated result will be like that:
- Each day a library/project will start from (Year, Month, Day, Daily Day Incremental)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



