Click here to Skip to main content
Click here to Skip to main content

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

, 23 Apr 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Herbrandson
Software Developer (Senior) Scratch Audio
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralCompile Pinmemberbryonmvi22-Apr-08 10:37 
GeneralThanks PinmemberAnthony Bouch15-Apr-07 14:50 
QuestionMSBuild? PinmemberChris Richner13-Apr-07 17:45 
AnswerRe: MSBuild? PinmemberHerbrandson17-Apr-07 6:57 
GeneralRe: MSBuild? PinmemberChris Richner17-Apr-07 8:41 
GeneralRe: MSBuild? PinmemberShaun Wilde16-May-07 14:00 
Generalif only i had known this... Pinmemberjburrow11-Apr-07 4:15 
QuestionGreat! Where is the rest of it? Pinmemberflipdoubt7-Apr-07 5:50 
AnswerRe: Great! Where is the rest of it? Pinmemberdabs9-Apr-07 5:24 
AnswerRe: Great! Where is the rest of it? PinmemberHerbrandson9-Apr-07 6:08 
GeneralCool PinmvpJosh Smith6-Apr-07 19:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 23 Apr 2007
Article Copyright 2007 by Herbrandson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid