Click here to Skip to main content
6,822,123 members and growing! (17,813 online)
Email Password   helpLost your password?
Languages » C# » How To     Beginner License: The Code Project Open License (CPOL)

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

By Herbrandson

A simple solution to keeping multiple projects version numbers in sync
C#, VB7.x, VB8.0, VB9.0, Windows, .NET1.0, .NET1.1, .NET2.0, .NET3.0VS.NET2003, VS2005, Dev
Posted:6 Apr 2007
Updated:23 Apr 2007
Views:20,445
Bookmarked:49 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
24 votes for this article.
Popularity: 6.31 Rating: 4.57 out of 5

1
1 vote, 4.2%
2
2 votes, 8.3%
3
1 vote, 4.2%
4
20 votes, 83.3%
5

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)

About the Author

Herbrandson


Member

Occupation: Software Developer (Senior)
Company: AXT Software
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralCompile Pinmemberbryonmvi10:37 22 Apr '08  
GeneralThanks PinmemberAnthony Bouch14:50 15 Apr '07  
GeneralMSBuild? PinmemberChris Richner17:45 13 Apr '07  
GeneralRe: MSBuild? PinmemberHerbrandson6:57 17 Apr '07  
GeneralRe: MSBuild? PinmemberChris Richner8:41 17 Apr '07  
GeneralRe: MSBuild? PinmemberShaun Wilde14:00 16 May '07  
Generalif only i had known this... Pinmemberjburrow4:15 11 Apr '07  
GeneralGreat! Where is the rest of it? Pinmemberflipdoubt5:50 7 Apr '07  
GeneralRe: Great! Where is the rest of it? Pinmemberdabs5:24 9 Apr '07  
GeneralRe: Great! Where is the rest of it? PinmemberHerbrandson6:08 9 Apr '07  
GeneralCool PinmvpJosh Smith19:12 6 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 23 Apr 2007
Editor: Genevieve Sovereign
Copyright 2007 by Herbrandson
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project