Friday, September 05, 2008

How to use consistent version numbering across multiple projects

If you have many C# projects, you may want all your assemblies to have the same version numbers. Remember that the version numbers are defined in the ‘AssemblyInfo.cs’ file in each project.

There are several mechanisms to achieve this:

  • Programmatically change all the ‘AssemblyInfo.cs’-files to update the version number.
    You could write a tool that updates the version number of all your projects. This tool could then be integrated in your build process. This is cumbersome and often not so simple if your projects are checked in a source control system.
  • Define the version-numbers in a single file that is used by all your projects. This technique is explained in this article.
  • Define the version-numbers in a separate assembly that is referenced by all projects.

In this post I will elaborate on the third mechanism (because I’ve never seen this explained anywhere else).

Create a new C# project (called Metadata.csproj) that contains all the metadata that is shared across your projects. This project is very simple and contains only 1 class:

   1: namespace KristofVerbiest
   2: {
   3:     public static class ProjectMetadata
   4:     {
   5:         public const string FileVersion = "1.5.0.1";
   6:         public const string CompanyName = "Kristof Verbiest";
   7:         public const string Copyright = "Copyright © Kristof Verbiest 2008";
   8:     }
   9: }

Now you can reference this assembly from all your other C# projects, and you can use the data from the ‘AssemblyInfo.cs’ files like this:

   1: [assembly: AssemblyFileVersion(ProjectMetadata.FileVersion)]
   2: [assembly: AssemblyCompany(ProjectMetadata.CompanyName)]
   3: [assembly: AssemblyCopyright(ProjectMetadata.CopyRight)]

Note that the ‘Metadata.dll’ assembly is used by the compiler to fill in the correct metadata. However this assembly is not needed at runtime! So you don’t need to deploy this assembly to your users, it is only needed by the compiler.

3 comments:

Roy Dallal said...

You don't really need a metadata project. Visual Studio (at least 2005) allows you to add a code file as a link.
What we do is put a file under the Solution Items - SolutionInfo.cs
which contains all the constant configurations and add a link to it.
You can read more about it
here

Kiki said...

Sometimes I need to have different versions for sub-projects, but then at times need to sync them again.

I feel that this VS add-on gives me exactly the features and control over versions that I need: http://www.codeproject.com/KB/macros/versioningcontrolledbuild.aspxudi

Miloš Falta said...

Funguje spolehlivě.
Děkuji