Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Keep Your Sanity and Multiple Projects Version Numbers in Sync

0.00/5 (No votes)
23 Apr 2007 1  
A simple solution to keeping multiple projects version numbers in sync

Introduction: Versioning Troubles

In the project I'm currently working on, we have multiple projects in one solution. One of the problems we've faced is how to keep all the version numbers (generally found in the ApplicationInfo file) in sync. Ok, so "problem" might be too strong of a word. It's more of an annoyance, but nonetheless, it would be nice to have some sort of automated solution. The other day, I downloaded some sample code for an unrelated issue, but they had a really nice solution to the versioning puzzle. I've now added this solution to my current project, with a few little wrinkles, and it seems to be working really well (we won't know for sure until the next release). [NOTE: this solution assumes you want the same version number for each project in the solution]

The Short of It

Here's what you need to do:

  1. Remove these attributes from the AssemblyInfo files in each project in the solution:
    [assembly: AssemblyVersion("1.0.0.0")]
    [assembly: AssemblyFileVersion("1.0.0.0")]
  2. Create a new file in the root directory of the solution (I named mine VersionInfo).
  3. Add the attributes that we removed from the AssemblyInfo file to your new file (you'll also need to add a reference to the System.Reflection namespace).
  4. On each project in the solution, right click the project and select Add->Existing Item.
  5. HERE'S THE KEY. Browse to your newly created version file, but instead of clicking the "Add" button, click the little down arrow next to the word "Add" and then click "Add as link" from the menu it drops down.

What Did We Just Do?

Normally when you add an existing file, the IDE copies the selected file to the current directory. By selecting "Add as link," what we've done is link the file in from its original location (yes, I know you figured that out from the name "Add as link"). Now when we build our solution, each project will compile in the exact same file (VersionInfo in my case) , thus giving each project the same version number.

The Next Step

To make this really cool (well, at least I think it's cool) I've created a build script to automate the process of creating the release build. The script prompts for the version number of the release and updates the version file before doing the build. Here's a snippet of my script (parts of this script were omitted to protect the innocent)...

Dim versionNumber 
Set shell = CreateObject("WScript.Shell") 
Set fileSystemObject = CreateObject("Scripting.FileSystemObject") 

sub Main() 
    Echo "Getting version number" 
    versionNumber = InputBox("What version is this build?", "Version") 
    UpdateFileVersion versionNumber 

    CommitToVersionControl 
    GetLatestFromVersionControl 

    Echo "Building" 
    RunCommand _
        """C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe""_
        SolutionName.sln /Rebuild Release", "Failed to build" 

    RunUnitTests 
    CreateZipFile 

    MsgBox("Done.") 
end sub 

sub Echo(message) 
    WScript.Echo message 
end sub 

sub UpdateFileVersion(versionNumber) 
    Set file = fileSystemObject.OpenTextFile("Version.cs", 2) 

    file.WriteLine "using System.Reflection"+ vbcrlf + _ 
        vbcrlf + _ "[assembly:AssemblyVersion""" + _
        versionNumber + ".0.0"")]" + _
        vbcrlf + "[assembly:AssemblyFileVersion""" + _
        versionNumber + ".0.0"")]" 

    file.Close 
end sub 

sub RunCommand(command, failMessage) 
    result = shell.Run(command, 1 , 1) 
    TestResult result, failMessage 
end sub 

sub TestResult(result, failMessage) 
    if result <> 0 then 
        MsgBox(failMessage) 
        WScript.Quit 
    end if 
end sub 

Main 

Conclusion

I now have a very simple and very automated versioning process. It makes sure that our projects' version numbers are never out of sync and, more importantly, makes sure that I don't forget to update the version numbers when I do a final release build. I hope this helps.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here