C# 如何动态更新 Assemblyinfo.cs 中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14254554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:11:00  来源:igfitidea点击:

How to update the Value in Assemblyinfo.cs dynamically

c#wpf.net-4.0assembliesassemblyinfo

提问by Simsons

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

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

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

由于我无法在 Assemblyinfo.cs 中编写任何代码,我将如何更新 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))]

采纳答案by VinayC

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.

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

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

您可以为此创建 MS Build 任务:请参阅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

命令行实用程序 (SubWCRev) 也可用于基于关键字的替换($WCREV$ 用于修订) - 请参阅此处的示例: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.

最后,您可能可以推出您的自定义代码(进行正则表达式搜索/替换)以执行某些(包括我在内)所做的相同操作 - 请参见此处的一个示例:http: //www.codeproject.com/Articles/168528/自动保持装配修订同步

回答by Roger Lipscombe

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 Packhas an AssemblyInfotask.

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

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

为了从 Subversion 获取修订版,MSBuild 扩展包也有一个任务

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.

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

回答by bvj

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

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

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

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

AssemblyInfo.cs(setup once specifying the VersionInfo constants)

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(dynamically generated by the ant build system)

VersionInfo.cs (由蚂蚁构建系统动态生成)

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

version.xml (蚂蚁导入 - 剪断)

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

version.properties (指定产品版本信息的 ant 属性文件)

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)

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 Extensions(none!)

MSBuild 扩展(无!)

<!-- none, not required -->

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

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