I have created the template for sharedAssemblyInfo.tt its updating version number only once for the first build or rebuild. If I change some code and try to build the whole project it’s not reflecting the version number.
-
1Maybe this helps you: https://stackoverflow.com/questions/1646580/get-visual-studio-to-run-a-t4-template-on-every-build – mu88 Nov 18 '20 at 12:43
2 Answers
If you want to increment the build number every time you build the project regardless of the selected configuration you can follow the steps below and you can also modify it manually in Application ➤ Assembly Information
Code below will read the existing AssemblyInfo.cs
file, and use regex to find the AssemblyVersion
information and then increment the revision and build numbers based on input from TextTransform.exe
.
- Delete your existing
AssemblyInfo.cs
file. - Create a
AssemblyInfo.tt
file in its place. Visual Studio should createAssemblyInfo.cs
and group it with the T4 file after you save the T4 file.
Code:
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs"));
Regex pattern = new Regex("AssemblyVersion\\(\"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<revision>\\d+)\\.(?<build>\\d+)\"\\)");
MatchCollection matches = pattern.Matches(output);
if( matches.Count == 1 )
{
major = Convert.ToInt32(matches[0].Groups["major"].Value);
minor = Convert.ToInt32(matches[0].Groups["minor"].Value);
build = Convert.ToInt32(matches[0].Groups["build"].Value) + 1;
revision = Convert.ToInt32(matches[0].Groups["revision"].Value);
if( this.Host.ResolveParameterValue("-","-","BuildConfiguration") == "Release" )
revision++;
}
#>
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;
// General Information
[assembly: AssemblyTitle("Insert title here")]
[assembly: AssemblyDescription("Insert description here")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Insert company here")]
[assembly: AssemblyProduct("Insert product here")]
[assembly: AssemblyCopyright("Insert copyright here")]
[assembly: AssemblyTrademark("Insert trademark here")]
[assembly: AssemblyCulture("")]
// Version informationr(
[assembly: AssemblyVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]
<#+
int major = 1;
int minor = 0;
int revision = 0;
int build = 0;
#>
- Add this to your pre-build event:
"%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"
Let me know if something won't work with this solution as I use it often in my projects.
#Edit:
For many projects you can try with VS extension which maybe would be helpful in this case. There is an extension that my colleagues used. It can be found in Visual Studio Marketplace, in the Releases area, and inside Visual Studio under "Extensions and Updates" called "Intentional Solution Version Editor".

- 526
- 3
- 10
-
My solution is having multiple projects, so instead of creating a template for AssemblyInfo for each project, I have created the template for sharedAssemblyInfo.tt – Nikhil Nov 18 '20 at 13:06
-
@Nikhil I have updated my post, not sure if it will help but check it out – FilipA Nov 18 '20 at 13:14
If you use gitlab
you could use GitVersionTask (https://www.nuget.org/packages/GitVersionTask).
On every commit, it automatically increases the Version
of your build and even rename the output file. You just have to install the nuget to your project and you're done.
Example output for branch test-complex-udt
on 2
commits:
HtOPC.Local.1.5.2-test-complex-udt0002

- 4,717
- 1
- 34
- 77