Joelty
Joelty

Reputation: 2009

Generating version for every csproj in solution and accessing it from code

Basically my solution contains e.g 10 csproj files

I'd want to every of them being versioned and then I'd display it in my app (asp netcore mvc) on some page. It'd list of all csproj name with its version (which can be generated by app or something)

e.g

Services 2019.05.01

I thought about making public static Dictionary<string, string> in a .csproj which all other csproj reference from and on something like "csproj entry point" it'd perform .Add("name of csproj", "some version based on data")

Has anybody an better idea how to achieve something like that?

Upvotes: 1

Views: 586

Answers (1)

Joelty
Joelty

Reputation: 2009

Original answer: https://stackoverflow.com/a/50607951/10522960

Backup:

For .NET Core projects, I adapted Postlagerkarte's answer to update the assembly Copyright field with the build date.

Directly Edit csproj

The following can be added directly to the first PropertyGroup in the csproj:

<Copyright>Copyright © $([System.DateTime]::UtcNow.Year) Travis Troyer ($([System.DateTime]::UtcNow.ToString("s")))</Copyright>

Alternative: Visual Studio Project Properties

Or paste the inner expression directly into the Copyright field in the Package section of the project properties in Visual Studio:

Copyright © $([System.DateTime]::UtcNow.Year) Travis Troyer ($([System.DateTime]::UtcNow.ToString("s")))

This can be a little confusing, because Visual Studio will evaluate the expression and display the current value in the window, but it will also update the project file appropriately behind the scenes.

Solution-wide via Directory.Build.props

You can plop the <Copyright> element above into a Directory.Build.props file in your solution root, and have it automatically applied to all projects within the directory, assuming each project does not supply its own Copyright value.

<Project>
 <PropertyGroup>
   <Copyright>Copyright © $([System.DateTime]::UtcNow.Year) Travis Troyer ($([System.DateTime]::UtcNow.ToString("s")))</Copyright>
 </PropertyGroup>
</Project>

Directory.Build.props: Customize your build

Output

The example expression will give you a copyright like this:

Copyright © 2018 Travis Troyer (2018-05-30T14:46:23)

Retrieval

You can view the copyright information from the file properties in Windows, or grab it at runtime:

var version = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);

Console.WriteLine(version.LegalCopyright);

Upvotes: 2

Related Questions