How to update the Value in Assemblyinfo.cs dynamically

后端 未结 3 1412
情歌与酒
情歌与酒 2021-01-02 22:48

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 i

3条回答
  •  无人及你
    2021-01-02 23:36

    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 = @"support@MyCompany.com";
      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)

    // Shared Product Version Header${line.separator}
    // Automatically Generated: ${version.generated.time}${line.separator}
    // ReSharper disable CheckNamespace${line.separator}
    // ReSharper disable InconsistentNaming${line.separator}
    internal struct VersionInfo${line.separator}
    {${line.separator}
      public const string Company = @"${product.company}";${line.separator}
      public const string Copyright = @"${product.copyright}";${line.separator}
     
    }${line.separator}
    

    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
    product.email=support@MyCompany.com
    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!)

    
    

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

提交回复
热议问题