16

How to set current year in AssemblyInfo file?

I used

Instead of this:

<Assembly: AssemblyCopyright("Copyright 2012, Company Name.")> 

tried this:

<Assembly: AssemblyCopyright("Copyright" + DateTime.Now.Year.ToString() + ", Company Name.")> 

I get invalid constant error.

I don't want to use registry key entries, what is the optimum way of doing this? (so that when a user right clicks on EXE & looks for assembly information can see current year).

Thanks.

Sharpeye500
  • 8,775
  • 25
  • 95
  • 143
  • 4
    Add a calendar alert to Outlook or whatever you use for Jan 1 each year to go and change all of your copyright dates in your application. :) – Joe Apr 17 '12 at 17:50
  • The Copyright statement is a constant that is built into the generated executable's file info. Do you expect Windows to run a function in your program every time it tries to get the copyright? – M.Babcock Apr 17 '12 at 17:55
  • Do you wont a current year or year when build happen? – Antonio Bakula Apr 17 '12 at 17:58

7 Answers7

29

I saw something on another post (https://stackoverflow.com/a/827209/857307) that could really be helpful here.

Try this.

Add a new file to your source repository somewhere common to all of the projects in your solution. Call the file something like AssemblyCopyright.tt

In the file, add the following code for c#

<#@ template language="C#" #>
using System;
using System.Reflection;

[assembly: AssemblyCopyright("Copyright © CompanyName <#=DateTime.Now.Year#>")]

or vb

<#@ template language="VB" #>
<#@ output extension=".vb" #>
Imports System
Imports System.Reflection

<Assembly: AssemblyCopyright("Copyright © CompanyName <#=DateTime.Now.Year#>")> 

Then, remove the AssemblyCopyright attribute from each of your AssemblyInfo.cs files.

Finally, add a link to the AssemblyCopyright.tt file to each of your projects.

The template file will recreate a new AssemblyCopyright.cs file on every build with the correct year.

radbyx
  • 9,352
  • 21
  • 84
  • 127
5

Try this:

<Assembly: AssemblyCopyright("Copyright $([System.DateTime]::Now.ToString('yyyy')), Company Name.")> 

I haven't tried this exactly. Since i define some other parts of the assembly info via the .csproj using a WriteCodeFragment as described in this answer, I do it this way too - I define properties

  <!-- We started in 2021.  So, if it's still 2021 we just want to show '2021' -->
  <PropertyGroup Condition="$([System.DateTime]::Now.ToString('yyyy')) == '2021'">
    <CopyrightYears>2021</CopyrightYears>
  </PropertyGroup>
  <!-- But for later years we want a range.  E.g. in 2023 it will show '2021 - 2023' -->
  <PropertyGroup Condition="$([System.DateTime]::Now.ToString('yyyy')) != '2021'">
    <CopyrightYears>2021 - $([System.DateTime]::Now.ToString('yyyy'))</CopyrightYears>
  </PropertyGroup>

Then, within the task that updates the assembly info i have an ItemGroup including

<AssemblyAttributes Include="AssemblyCopyright">
  <_Parameter1>"Copyright © $(CopyrightYears) ..."</_Parameter1>
</AssemblyAttributes>

The after the ItemGroup I have the following within that task

    <MakeDir Directories="$(IntermediateOutputPath)" />
    <WriteCodeFragment Language="C#"
                         OutputFile="$(IntermediateOutputPath)Version.cs"
                         AssemblyAttributes="@(AssemblyAttributes)" />
    <ItemGroup>
        <Compile Include="$(IntermediateOutputPath)Version.cs" />
    </ItemGroup>

The functions you can use as properties are documented here

wonea
  • 4,783
  • 17
  • 86
  • 139
Adam
  • 6,539
  • 3
  • 39
  • 65
  • 1
    This solution also works for the new SDK-style csproj with automatically generated AssemblyInfo. Just add `Copyright $([System.DateTime]::Now.ToString('yyyy')), Company Name` in the first `PropertyGroup` of the project file. – pogosama May 27 '22 at 09:18
2

Probably the best way is to integrate this into your build process using tools like NAnt or MSBuild.

Here is an article that explains how to change your AssemblyInfo using MSBuild: Updating Assemblies with A Version Number.

BluesRockAddict
  • 15,525
  • 3
  • 37
  • 35
1

You can use NANT/MSBuild tasks to modify the AssemblyInfo.cs file like we do to change the Version of each assembly for every build.

For more information, visit http://msbuildtasks.tigris.org/

Cinchoo
  • 6,088
  • 2
  • 19
  • 34
1

Typically, for that kind of substitution, you use a pre-build step that invokes a script that automatically generates your assemblyInfo.cs file.

You can have a look at this thread : How can you find and replace text in a file using the Windows command-line environment?

Community
  • 1
  • 1
Vincent Hubert
  • 1,386
  • 9
  • 23
1

I had similar question, but for new style csproj file; that contains the file version info. I found a great answer here How to define current system date in post build event. Oddly, that is about a question that is somewhat different than my and the OP question. But, I think the accepted answer to that is what I want. Namely, $([System.DateTime]::Now.Year) in the csproj files evaluates to the current year.

steve
  • 1,021
  • 1
  • 14
  • 29
0

Here's my solution using a prebuild task. Should work on windows and unix. Will work with dotnet (if you run dotnet build, before dotnet run).

Add this to .csproj


  <!-- update assembly info -->
  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
    <Exec Condition="$(OS) == Windows_NT" Command="set TargetDir=$(TargetDir)&#xD;&#xA;..\scripts\updateAssemblyInfo.cmd" />
    <Exec Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' OR '$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))'" Command="export TargetDir=$(TargetDir)&#xD;&#xA;bash ../scripts/updateAssemblyInfo.sh" />
  </Target>

AssemblyInfo.base.txt

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

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCopyrightAttribute("Copyright © 2009 - <#=DateTime.Now.Year#> COMPANYNAME. All rights reserved.")]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("xxx")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version
//      Build Number
//      Revision
//
// [assembly: AssemblyVersion("1.0.0.0")]
// [assembly: AssemblyFileVersion("1.0.0.0")]

in scripts folder updateAssemblyInfo.cmd

for /f %%i in ('"powershell (Get-Date).ToString(\"yyyy\")"') do set currentYear=%%i
echo %currentYear%
powershell -Command "(gc 'AssemblyInfo.base.txt' -encoding "UTF8") -replace '<#=DateTime.Now.Year#>', '%currentYear%' | Out-File -encoding "UTF8" 'AssemblyInfo.cs'"

updateAssemblyInfo.sh

#!/bin/bash
year=$(date +%Y)
echo "Current Year : $year"
contents=$(cat AssemblyInfo.base.txt)
# echo $contents
echo $contents | awk -v yr=$year '{gsub("<#=DateTime.Now.Year#>",yr); print}' > AssemblyInfo.cs
lastlink
  • 1,505
  • 2
  • 19
  • 29