Create dynamic Assembly Version including date and random revision number

4.5k views Asked by At

I'm looking for a way of creating a dynamic Assembly Version for my ASP.NET MVC5 solution so that it would follow the format:

YYYY.M.D.XXXXX

For example:

2014.7.25.45261

Ideally I would like to do the following in AssemblyInfo.cs:

DateTime dt = DateTime.Today;
[assembly: AssemblyVersion(String.Format("{0}.{1}.{2}.*", dt.Year, dt.Month. dt.Day))]

I'm aware this isn't possible though.

Is there an alternative way of achieving this?

2

There are 2 answers

0
Ananke On BEST ANSWER

Versions are usually set by the build server however you can use a T4 template to generate your assembly info. I do this so I don't have to change dates manually. Add a file called AssemblyInfo.tt and add the following to it:

<#@ template language="C#" #>
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

[assembly: AssemblyCompany("My Company")]
[assembly: AssemblyProduct("My Product")]
[assembly: AssemblyCopyright("Copyright © My Company <#=DateTime.Now.Year#>")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: NeutralResourcesLanguage("en")]
[assembly: ComVisible(false)]
[assembly: AssemblyFileVersion("1.0.0.0")]
// etc.

Add the code you need to generate your version number. For more information see: http://msdn.microsoft.com/en-us/library/bb126445.aspx

0
nickgreek On

See following post if anyone need Visual Studio 2022 solution for that

How to Change AssemblyInfo.cs AssemblyVersion with date/time and increment revision daily by one in Visual Studio 2022

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:

  1. Each day a library/project will start from (Year, Month, Day, Daily Day Incremental)

Incremental daily

  1. Next Day increment from one again: Next Day increment from one again