How to update the Value in Assemblyinfo.cs dynamically

24,847

Solution 1

I am assuming that you are trying to generate some build system here - so essentially, you need to modify AssemblyInfo.cs file with svn number.

You can create MS Build task for the same: see http://florent.clairambault.fr/insert-svn-version-and-build-number-in-your-c-assemblyinfo-file

There is command line utility (SubWCRev) that can also be used for keyword based replacement ($WCREV$ for revision) - see the example here: http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-example.html

Finally, you can probably roll out your custom code (doing regex search/replace) to do the same which some (including me) has done - see one such example here: http://www.codeproject.com/Articles/168528/Automatically-keep-Assembly-revisions-in-sync-with.

Solution 2

I'm using an ant based build system, and had a similar question about updating AssemblyInfo.cs.

My build system already generates version.h files that are included into other projects at build time. For C#, I decided to use the generated VersionInfo.cs for version info injection into AssemblyInfo.cs.

AssemblyInfo.cs (setup once specifying the VersionInfo constants)

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany(VersionInfo.Company)]
[assembly: AssemblyProduct(VersionInfo.ProductName)]
[assembly: AssemblyCopyright(VersionInfo.Copyright)]

[assembly: Guid("12345678-1234-1234-1234-1234567890ab")]

[assembly: AssemblyVersion(VersionInfo.product.FileVersion)]
[assembly: AssemblyFileVersion(VersionInfo.sdk.FileVersion)]

VersionInfo.cs (dynamically generated by the ant build system)

// Shared Product Version Header
// Automatically Generated: Sat, 2 May 2015 15:09:09 EDT
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
internal struct VersionInfo
{
  public const string Company = @"My Company Corp";
  public const string Copyright = @"Copyright (c) 2015 My Company Corp.  All rights reserved.";
  public const string ProductName = @"My Software Service";
  public const string SpecialBuild = @"";
  public const string ProductConfiguration = @"";
  public const string ProductCode = @"MSS";
  public const string ProductUrl = @"www.MyCompany.com";
  public const string ProductEmail = @"[email protected]";
  public const string ProductVendor = @"My Company Corp";
  internal struct product
  {
    public const string FileVersion = @"1.5.0.240";
  }
  internal struct sdk
  {
    public const string FileVersion = @"1.4.5.222";
  }
  internal struct service
  {
    public const string FileVersion = @"1.3.2.142";
  }
}

version.xml (ant import - snipped)

<echo file="${artifacts.dir}\VersionInfo.cs" append="false">// Shared Product Version Header${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// Automatically Generated: ${version.generated.time}${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable CheckNamespace${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">// ReSharper disable InconsistentNaming${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">internal struct VersionInfo${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">{${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Company = @"${product.company}";${line.separator}</echo>
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">  public const string Copyright = @"${product.copyright}";${line.separator}</echo>
 <!-- other version info here -->
<echo file="${artifacts.dir}\VersionInfo.cs" append="true">}${line.separator}</echo>

version.properties (ant properties file specifying product version info)

product.company=My Company Corp
product.copyright=Copyright (c) 2015 My Company Corp.  All rights reserved.
product.website=www.MyCompany.com
[email protected]
product.vendor=My Company Corp

product=1.5.0.240
service=1.3.2.142
sdk=1.4.5.222

C# pre-build event (copies auto-generated VersionInfo.cs to project Properties directory)

@ECHO OFF
SETLOCAL
SET ARTDIR=$(ProjectDir)..\..\..\MySoftwareService\installer
SET VERCS=%ARTDIR%\VersionInfo.cs
ECHO Updating %VERCS%
xcopy /D /Y "%VERCS%" "$(ProjectDir)Properties"

MSBuild Extensions (none!)

<!-- none, not required -->

BTW, my version scheme is: major.minor.revision.build which isn't what the AssemblyInfo.cs file specifies.

Solution 3

You'll need to do this during your build process.

Since you're using Visual Studio, you're already using MSBuild, in which case the MSBuild Extension Pack has an AssemblyInfo task.

For getting the revision from Subversion, the MSBuild Extension Pack has got a task for that as well.

Alternatively, if you're using TeamCity for continuous integration (CI), it has an "AssemblyInfo patcher" build feature. I guess that most other CI systems will have something similar.

Share:
24,847
Simsons
Author by

Simsons

Love to write programs . Still learning and trying to explain the code to my self and others.

Updated on July 09, 2022

Comments

  • Simsons
    Simsons almost 2 years

    I have writen a program which gets the value from SVN repository . Now I want to update the AssemblyFileversion with that value.

    As I am not able to write any code inside Assemblyinfo.cs , how will I update the value of AssemblyFileVersion.

    I want to achieve something like this

    ..........................
    // Version information for an assembly consists of the following four values:
    //
    //      Major Version
    //      Minor Version 
    //      Build Number
    //      Revision
    //
    // You can specify all the values or you can default the Build and Revision Numbers 
    // by using the '*' as shown below:
    // [assembly: AssemblyVersion("1.0.*")]
    
     SvnInfoEventArgs info;
                        Uri repoURI = new Uri("ServerAddress");
                        svnClient.GetInfo(repoURI, out info);
    
    
    [assembly: AssemblyVersion("1.0.0.0")]
        [assembly: AssemblyFileVersion(String.Format("{0}.{1}.{2}. {3}",
                                              major,minor,build,info.Revision))]
    
  • Simsons
    Simsons over 11 years
    I have added MsBuildTask and updated the ToolPath="$(ProgramFiles)\TortoiseSVN\bin" , but my assemblyinfo is getting updated to 1.0.-1 ! Is it because of Tortoise SVN
  • VinayC
    VinayC over 11 years
    @Simsons, check if you have svn.exe in the bin folder. If I remember correctly, while installing Tortoise SVN, you have to select command line client tools for installation (run the installer again to get them). Or else you can always download svn command line client.
  • Simsons
    Simsons over 11 years
    Thanks Vinay, The problem is I was running the script on afolder not checkedout from SVN. Commenting so that any one landing on this page should not be confused with my previous comments
  • Simply G.
    Simply G. about 8 years
    To clarify Simsons comment, we had the same problem since our Teamcity was configured to just checkout the code (it was not actually in a SVN managed folder) and thus this solution did not work. Without a folder managed by SVN, the command line svn executables (we used Tortoise) failed. We had to change the configuration to make it check out and keep managed SVN folder on the build agent machine. Thus solving the 1.0.-1 issue.