Thursday, April 06, 2006

Consistent Version Numbers Across All Assemblies

I came across a simple, but useful, technique the other day. It keeps the version numbers of all assemblies consistent when building your Visual Studio solution. It works with either 2003 or 2005 flavors. I’ll describe the C# 2005 version since I have it installed on my machine, but 2003 works the same way.

Have you ever looked at what’s inside AssemblyInfo.cs? Go ahead and take a look; there is one in each project. There are a few using directives at the top, and the rest of the file contains assembly level attributes. Some of them will vary from assembly to assembly, such as AssemblyTitle and AssemblyDescription. Some of the others will be the same from one project to another in the same solution. These are “solution level” attributes. AssemblyCompany and AssemblyCopyright are examples of attributes that probably should be solution level attributes. AssemblyVersion is also one of the attributes included in the file. We will work with it in just a bit.

The first task is to take a look at the attributes already in the file and any additional attributes you want to add, and decide which ones are specific to a particular assembly and which ones should be solution level. Now add a new file called SolutionInfo.cs to the solution. A new folder named Solution Items will be created under the solution and SolutionInfo.cs will be in the folder. Copy all the solution level attributes into it and remove them from all the AssemblyInfo.cs files. For example, copy the AssemblyCompany attribute to the SolutionInfo.cs file and delete it from the AssemblyInfo.cs file for each project in the solution. There should be no overlap of attributes between AssemblyInfo.cs and SolutionInfo.cs. If the same attribute appears in both file, you will probably get a compile error.

We now have a reduced set of the attributes in each project and the remainder of the attributes in the Solution Items folder. What we need is to have all the attributes from the SolutionInfo.cs file in each project. If we add the SolutionInfo.cs file to each project we are right back where we started and haven’t really accomplished anything. What we really need, is the contents of the SolutionInfo.cs file in each project without the actual file. It turns out there is an easy way to do just that.

For each project, add an existing item and navigate to the folder containing SolutionInfo.cs.



Instead of double-clicking the file or clicking on the Add button, use the little arrow to the right of the Add button to drop down a list of options. Select Add As Link from the drop down list. That adds the following entry to your project files.

<Compile Include="..\SolutionInfo.cs">
<Link>SolutionInfo.cs</Link>
</Compile>;

The compiler will include that file when it compiles each project, but the physical file is in just one location. Make sure the AssemblyVersion attribute is in the SolutionInfo.cs file and not in the AssemblyInfo.cs file in each project. If your application is at version 1.0 and you want to increase the version number to 1.1, just change it in the SolutionInfo.cs file and as each project is compiled, the version number is set to 1.1.

Try it, I think you will like it.

6 Comments:

At 7:40 AM, Blogger Shaun said...

Cheers Joe, very handy and very easy to follow.

 
At 10:30 AM, Blogger SandpointGuy said...

Under Visual Studio 2008, when I do the Add Existing Item on a project, and navigate to the solutioninfo.cs, the button doesnt have the down arrow. Ideas?

 
At 10:50 AM, Blogger SandpointGuy said...

More on the above: I have 2 projects in the solution, a class library and a web site. Add Link appears in the class library. It does NOT appear for the web site (it does appear in 2005 web sites).

 
At 4:05 AM, Blogger Jeppe said...

Nice.

Does this work with setup projects as well?

They don´t have an assembly file so will it also replace their version number?

 
At 2:40 PM, Blogger AndyG said...

Brilliant, works a treat. Thanks for sharing this Joe.

 
At 4:35 PM, Blogger Jimmy Neutron said...

Thanks for this. I like to manually edit the csproj file and change the link that got added from "SolutionInfo.cs" to "Properties\SolutionInfo.cs" so that it displays right next to AssemblyInfo.cs.

 

Post a Comment

<< Home