【问题标题】:How to update the Value in Assemblyinfo.cs dynamically如何动态更新 Assemblyinfo.cs 中的值
【发布时间】:2012-12-24 15:23:53
【问题描述】:

我编写了一个从 SVN 存储库获取值的程序。现在我想用该值更新 AssemblyFileversion。

由于我无法在 Assemblyinfo.cs 中编写任何代码,我将如何更新 AssemblyFileVersion 的值。

我想实现这样的目标

..........................
// 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))]

【问题讨论】:

标签: c# wpf .net-4.0 assemblies assemblyinfo


【解决方案1】:

您需要在构建过程中执行此操作。

由于您使用的是 Visual Studio,因此您已经在使用 MSBuild,在这种情况下,MSBuild Extension Pack 有一个 AssemblyInfo 任务。

为了从 Subversion 获取修订,MSBuild 扩展包也有 a task for that

或者,如果您使用 TeamCity 进行持续集成 (CI),它具有“AssemblyInfo 修补程序”构建功能。我猜大多数其他 CI 系统都会有类似的东西。

【讨论】:

    【解决方案2】:

    我假设您正在尝试在这里生成一些构建系统 - 所以本质上,您需要使用 svn 编号修改 AssemblyInfo.cs 文件。

    您可以为此创建 MS Build 任务:请参阅 http://florent.clairambault.fr/insert-svn-version-and-build-number-in-your-c-assemblyinfo-file

    有一个命令行实用程序 (SubWCRev) 也可用于基于关键字的替换($WCREV$ 用于修订版) - 请参见此处的示例:http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev-example.html

    最后,您可能可以推出您的自定义代码(进行正则表达式搜索/替换)以执行某些人(包括我)所做的相同操作 - 请在此处查看一个这样的示例:http://www.codeproject.com/Articles/168528/Automatically-keep-Assembly-revisions-in-sync-with

    【讨论】:

    • 我添加了 MsBuildTask 并更新了 ToolPath="$(ProgramFiles)\TortoiseSVN\bin" ,但我的 assemblyinfo 正在更新到 1.0.-1 !是不是因为 Tortoise SVN
    • @Simsons,检查 bin 文件夹中是否有 svn.exe。如果我没记错的话,在安装 Tortoise SVN 时,您必须选择命令行客户端工具进行安装(再次运行安装程序以获取它们)。否则你可以随时下载 svn 命令行客户端。
    • 感谢 Vinay,问题是我在未从 SVN 签出的文件夹上运行脚本。发表评论,以免任何登陆此页面的人与我以前的 cmets 混淆
    • 为了澄清 Simsons 的评论,我们遇到了同样的问题,因为我们的 Teamcity 被配置为只签出代码(它实际上不在 SVN 托管文件夹中),因此这个解决方案不起作用。如果没有 SVN 管理的文件夹,命令行 svn 可执行文件(我们使用 Tortoise)会失败。我们必须更改配置以使其签出并在构建代理机器上保留托管的 SVN 文件夹。从而解决了 1.0.-1 的问题。
    【解决方案3】:

    我正在使用基于 ant 的构建系统,并且有一个关于更新 AssemblyInfo.cs 的类似问题。

    我的构建系统已经生成了 version.h 文件,这些文件在构建时包含在其他项目中。对于 C#,我决定使用生成的 VersionInfo.cs 将版本信息注入 AssemblyInfo.cs

    AssemblyInfo.cs (在指定 VersionInfo 常量后设置)

    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 (由ant构建系统动态生成)

    // 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)

    <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属性文件)

    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# 预构建事件 (将自动生成的 VersionInfo.cs 复制到项目属性目录)

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

    MSBuild 扩展 (无!)

    <!-- none, not required -->
    

    顺便说一句,我的版本方案是:major.minor.revision.build 这不是 AssemblyInfo.cs 文件指定的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 2019-08-02
      • 2010-09-12
      • 1970-01-01
      • 2020-12-20
      相关资源
      最近更新 更多