'Current year in AssemblyInfo copyright

Is there a possibility to include the current year via DateTime.Now.Year in the AssemblyInfo.cs-file?

I have tried:

[assembly: AssemblyCopyright("Copyright " + DateTime.Now.Year)]

But it seems the argument can only be a constant expression.



Solution 1:[1]

As has been said, you cannot put values into attributes which are not constants. DateTime.Now is not a constant value and therefore cannot be used in an attribute.

If you especially wanted this behaviour, you can add a script a pre-compile step which inserts the date into files.

Personally, given the rate at which the year changes, it would be time poorly spent creating an autonomous task to do this. I have numerous projects which have "2009" in their Assembly Info. I have a task in my list to complete in the new year, to run a Regex tool to find and replace all instances of "2009" with "2010" in the AssemblyInfo.cs files.

I would submit that this is a lot less work than integration into your build process.

Solution 2:[2]

This can be automated, but you'll need to use a method which manipulates the AssemblyInfo.cs file pre-build.

Should you be doing this though?

A term of copyright doesn't restart when you rebuild your code. If the copyright is currently 2009 it should remain at 2009 regardless of the current year, unless you make significant* code changes in a later year.

*For the value of 'significant' you need to consult a lawyer, not a software developer.

Solution 3:[3]

This is not possible because in .NET attributes may contain only constant expressions. You could use a before compile step that modifies the file and inserts current year.

Solution 4:[4]

Use @ {0} Microsoft in the AssemblyInfo.cs file and use string.Format in the server side to replace {0} with the current year value.

Solution 5:[5]

It is possible using template files. Have a look at my answer on the following thread

Dynamically display current year in assembly info

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Turner
Solution 2
Solution 3 Darin Dimitrov
Solution 4 Thambu
Solution 5 Community