Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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

Dynamically display current year in assembly info

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top