Skip to content

Instantly share code, notes, and snippets.

@adituv
Last active August 29, 2015 14:27
Show Gist options
  • Save adituv/d55016205b932551f6ef to your computer and use it in GitHub Desktop.
Save adituv/d55016205b932551f6ef to your computer and use it in GitHub Desktop.
Visual Studio 2012 - Automatically incrementing version for C#

This requires Microsoft Visual Studio 2012 SDK and Microsoft Visual Studio 2012 Visualization & Modeling SDK, which come separately from Visual Studio 2012. Other version combinations are probably possible, and maybe compatible, but untested. The core of the method is this:

  • Add AssemblyInfo.tt to MyProject\Properties, filling out the "Your xxxxxx" sections.
  • Open up the project file (e.g. MyProject.csproj) in a text editor (e.g. Notepad++), and locate the line that defines the build targets for the language ("import project... microsoft.csharp.targets")
  • Insert the contents of MyProject.csproj there
  • Finally, if you build the AssemblyInfo.tt template within Visual Studio, make sure to set AssemblyInfo1.cs to Build Action: None in the File Properties window. This will fix any errors about duplicate definitions.

Now, when building in the Release configuration, the third part of the version number is incremented and the fourth reset; when building in any other configuration, the fourth part of the version number is incremented.

To force a version bump, edit the AssemblyInfo.cs file directly, setting the numbers to the version you want; the templater will automatically increment from where you put it from that point onward.

A simpler alternative is to use Microsoft's default version "incrementing": edit the existing AssemblyInfo.cs file and remove the AssemblyFileVersion line, then edit the AssemblyVersion line to: [assembly: AssemblyVersion("M.m.*")] where M and m are your major and minor version respectively. This will generate a version number like 1.3.3912.482 where the third part is the number of days since 2000-01-01, and the fourth part is half the number of seconds since midnight.

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ parameter type="System.String" name="Configuration" #>
<#
try {
string currentDirectory = Path.GetDirectoryName(Host.TemplateFile);
string assemblyInfo = File.ReadAllText(Path.Combine(currentDirectory,"AssemblyInfo.cs"));
Regex pattern = new Regex("AssemblyVersion\\(\"\\d+\\.\\d+\\.(?<revision>\\d+)\\.(?<build>\\d+)\"\\)");
MatchCollection matches = pattern.Matches(assemblyInfo);
if (Configuration == "Release") {
build = 1;
revision = Convert.ToInt32(matches[0].Groups["revision"].Value) + 1;
} else {
build = Convert.ToInt32(matches[0].Groups["build"].Value) + 1;
}
}
catch( Exception ) { }
#>
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;
// General Information
[assembly: AssemblyTitle("YourProgramName")]
[assembly: AssemblyDescription("Your Program Description")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Your Name/Company Name")]
[assembly: AssemblyProduct("YourProgramName")]
[assembly: AssemblyCopyright("Your Copyright Message")]
[assembly: AssemblyTrademark("Your Trademark Message, if applicable")]
[assembly: AssemblyCulture("")]
// Version informationr(
[assembly: AssemblyVersion("1.0.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("1.0.<#= this.revision #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]
<#+
int revision = 0;
int build = 0;
#>
<!-- After '<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets">' -->
<PropertyGroup>
<!-- Get the Visual Studio version – defaults to 10: -->
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<!-- Keep the next element all on one line: -->
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<PropertyGroup>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
</PropertyGroup>
<PropertyGroup>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
<ItemGroup>
<T4ParameterValues Include="Configuration">
<Value>$(Configuration)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment