如何在AssemblyInfo文件中设置当前年份?
我用了
而不是这个:
<Assembly: AssemblyCopyright("Copyright 2012, Company Name.")> 
试过这个:
<Assembly: AssemblyCopyright("Copyright" + DateTime.Now.Year.ToString() + ", Company Name.")> 
我得到无效的常量错误.
我不想使用注册表项,这样做的最佳方法是什么?(这样当用户右键单击EXE并查找汇编信息时可以看到当前年份).
谢谢.
小智 24
我在另一篇文章(/sf/answers/57904661/)上看到了一些在这里真有用的内容.
试试这个.
将新文件添加到源存储库中解决方案中所有项目通用的某个位置.调用类似AssemblyCopyright.tt的文件
在文件中,为c#添加以下代码
<#@ template language="C#" #>
using System;
using System.Reflection;
[assembly: AssemblyCopyright("Copyright © CompanyName <#=DateTime.Now.Year#>")]
或者vb
<#@ template language="VB" #>
<#@ output extension=".vb" #>
Imports System
Imports System.Reflection
<Assembly: AssemblyCopyright("Copyright © CompanyName <#=DateTime.Now.Year#>")> 
然后,从每个AssemblyInfo.cs文件中删除AssemblyCopyright属性.
最后,为每个项目添加一个AssemblyCopyright.tt文件的链接.
模板文件将在每个构建时使用正确的年份重新创建一个新的AssemblyCopyright.cs文件.
尝试这个:
\n<Assembly: AssemblyCopyright("Copyright $([System.DateTime]::Now.ToString(\'yyyy\')), Company Name.")> \n我还没有具体尝试过这个。由于我使用此答案WriteCodeFragment中所述的.csproj 定义了程序集信息的其他部分,所以我也这样做 - 我定义属性
  <!-- We started in 2021.  So, if it\'s still 2021 we just want to show \'2021\' -->\n  <PropertyGroup Condition="$([System.DateTime]::Now.ToString(\'yyyy\')) == \'2021\'">\n    <CopyrightYears>2021</CopyrightYears>\n  </PropertyGroup>\n  <!-- But for later years we want a range.  E.g. in 2023 it will show \'2021 - 2023\' -->\n  <PropertyGroup Condition="$([System.DateTime]::Now.ToString(\'yyyy\')) != \'2021\'">\n    <CopyrightYears>2021 - $([System.DateTime]::Now.ToString(\'yyyy\'))</CopyrightYears>\n  </PropertyGroup>\n然后,在更新程序集信息的任务中,我有一个ItemGroup包括
<AssemblyAttributes Include="AssemblyCopyright">\n  <_Parameter1>"Copyright \xc2\xa9 $(CopyrightYears) ..."</_Parameter1>\n</AssemblyAttributes>\n之后ItemGroup我在该任务中有以下内容
    <MakeDir Directories="$(IntermediateOutputPath)" />\n    <WriteCodeFragment Language="C#"\n                         OutputFile="$(IntermediateOutputPath)Version.cs"\n                         AssemblyAttributes="@(AssemblyAttributes)" />\n    <ItemGroup>\n        <Compile Include="$(IntermediateOutputPath)Version.cs" />\n    </ItemGroup>\n您可以用作属性的函数记录在此处
\n| 归档时间: | 
 | 
| 查看次数: | 9294 次 | 
| 最近记录: |