Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#

The Right Way to Version Your Assemblies

Rate me:
Please Sign up or sign in to vote.
5.00/5 (33 votes)
13 Feb 2012CPOL5 min read 143.3K   56   44
How you can structure your solution and project files in Visual Studio to make updating your assemblies' version number much easier

No, I'm not talking about whether you have Major, Minor, Patch, and Build numbers. I'm not talking about when you increment which number. My suggestion is to do whatever works for you in those cases, though I reserve the right to blog about that in the future. What I am talking about is how you can structure your solution and project files in Visual Studio to make updating your assemblies' version number much easier. This blog post makes some assumptions:

  • You are using version numbers in your assemblies
  • You are using a recent version of Visual Studio (I know these steps work with both Visual Studio 2008 and 2010)
  • You have multiple assemblies in one Visual Studio solution
  • You want to update all of your assemblies' version numbers at the same time and to the same version number

1-OriginalSolutionExplorer

Those assumptions cover a vast majority of the projects I've worked on, but I'll admit that there may be projects out there that might not fit into the mold I've set forth. I'm OK with that. This blog post isn't for you if your mold is different. So let's get started. I created a solution file which contains several projects. There's an MVC2 web application project, its associated test project, a business objects project, and a data access layer project. These are all empty projects for this example—only the structure of the solution and projects is important for my purposes. Solution explorer for this project is shown here. The first step is to separate out the version information (and any other information that is common from assembly to assembly if you wish, like company name, product name, and so on) into its own file. Open up one of the AssemblyInfo.cs files to see what I'm talking about.

2-OriginalAssemblyInfo

Notice that the settings are assembly attributes. Some people don't realize that you don't have to put all the assembly attributes in one file. I'm going to take advantage of that fact to accomplish what I set out to do: change the version in one place and have all projects updated at once. The other key is using a solution-level file and linking to is from each project. Visual Studio supports files at the solution level as well as at the project level. You can put anything you want at the solution level and it will be carried along with the solution, though it won't get included in any output unless you write some custom build rules to grab the file and do something with it. On some projects, I've included a Word document about the solution and its projects as a solution level file. For this example, we're going to create a new C# source file and include it at the solution level. I'm going to call this new file, "VersionInfo.cs".

3-CreateVersionInfoCs

  • Step 1: Create a new file and call it "VersionInfo.cs". Do this by choosing "File | New | File…" or press Ctrl-N in Visual Studio. Choose "Visual C# Class" as the file type.
  • Step 2: Erase everything in this file. Save it as VersionInfo.cs in the same folder as the solution file.
  • Step 3: Highlight the version information lines from the AssemblyInfo.cs file from earlier, cut it from that file (Ctrl-X) and paste it in VersionInfo.cs (Ctrl-V). Add "using System.Reflection;" to the top of VersionInfo.cs to avoid compilation errors.
  • Step 4: Add the VersionInfo.cs file to the solution by right-clicking on the solution file, choosing "Add | Existing Item…", then selecting "VersionInfo.cs" (find it in the solution's folder where you just saved it), and then click "Add". You should see a new "Solution Items" folder under your solution name with the VersionInfo.cs file in it (see screenshot).
  • Step 5: Add the VersionInfo.cs file to each project as a linked file. See below for instructions on how to do this.
  • Step 6: Remove the version information from the other projects' AssemblyInfo.cs files.

4-SolutionItemsFolder

At this point, you should be able to build your solution successfully, and each assembly should have the same version information—the information found in VersionInfo.cs. So what is a "Linked File" and how do I create one? Read on. Right-click on the project and choose "Add | Existing Item…", navigate up one folder level, and click once (don't double-click!) on the VersionInfo.cs file. [This assumes that your solution folder is one level up from your project folder!]

5-LinkedFile

Now instead of clicking "Add", click the little down-arrow next to the "Add" button. Choose "Add As Link". You should now see VersionInfo.cs in your project, but with a "shortcut" icon overlaid on top of the file's icon. Once you have completed this step for each project, you can build your solution without errors. Now to test it out. Build the solution and look in the bin folder (or "folders" if you haven't added the other projects as references in the web project) for the DLLs. Look at the version numbers. They should all read "1.0.0.0". Now change the version number in the VersionInfo.cs file information. Rebuild the solution, then check the version numbers. All of the assemblies should now have the new version number. Notice, however, that it is the "File Version" that you see when hovering over the DLL in Windows Explorer, so if you change only the "Assembly Version", then you'll have to go to the DLL's properties to see your change. [I must admit, I don't recall which version number is used when putting DLLs into the Global Assembly Cache. Please leave a comment if you have that information handy!]. So that is the procedure. If you have an automated build system, then you can make use of the fact that the version number is in its own file and update it automatically. Then each build will produce DLLs with distinct version numbers. That is a great way to identify the build that a DLL came from if you happen to get a report from the field that there is an error in a specific DLL. But I'm sure that's never happened to you…right?

This article was originally posted at http://swearpoint.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVB.NET? Pin
Member 1211415712-Jul-19 4:52
Member 1211415712-Jul-19 4:52 
AnswerRe: VB.NET? Pin
StevenLJackson127-Apr-20 16:31
StevenLJackson127-Apr-20 16:31 
GeneralPerfectly worked as expected Pin
Parameswara Reddy24-May-18 19:36
Parameswara Reddy24-May-18 19:36 
PraiseThank you Pin
ASISL28-Jul-17 2:06
ASISL28-Jul-17 2:06 
QuestionFor Shared Project? Pin
14Prajwal11-Aug-16 1:46
14Prajwal11-Aug-16 1:46 
AnswerRe: For Shared Project? Pin
StevenLJackson114-Feb-17 6:16
StevenLJackson114-Feb-17 6:16 
QuestionExcellent! Pin
Paul Conrad2-Oct-15 18:17
professionalPaul Conrad2-Oct-15 18:17 
Questionmany thanks, its was so easy and so well explained! Pin
Member 1155174521-Apr-15 11:38
Member 1155174521-Apr-15 11:38 
QuestionAuto-incrementation Pin
NHopkins24-Feb-15 10:19
NHopkins24-Feb-15 10:19 
This is extremely useful, thanks!

One issue with this is that it doesn't seem to support the #.#.#.* auto-incrementation model, at least for the file version. When I checked the .dll's Details, the file version showed the '*' character rather than the appropriate auto-incremented number. It's a relatively small price to pay, but it was a bit surprising and frustrating.
NewsGreat! Pin
nrgjack5-Feb-14 21:18
professionalnrgjack5-Feb-14 21:18 
GeneralRe: Great! Pin
StevenLJackson16-Feb-14 4:37
StevenLJackson16-Feb-14 4:37 
GeneralMy vote of 5 Pin
Sanoj T29-Jan-14 2:26
Sanoj T29-Jan-14 2:26 
QuestionNice Post!! Pin
Sanoj T29-Jan-14 2:25
Sanoj T29-Jan-14 2:25 
AnswerRe: Nice Post!! Pin
StevenLJackson129-Jan-14 3:04
StevenLJackson129-Jan-14 3:04 
QuestionBrilliant! Pin
dibley19734-Oct-13 4:46
dibley19734-Oct-13 4:46 
AnswerRe: Brilliant! Pin
StevenLJackson14-Oct-13 10:23
StevenLJackson14-Oct-13 10:23 
GeneralMy vote of 2 Pin
Singh Vijay Kumar7-Mar-13 23:40
professionalSingh Vijay Kumar7-Mar-13 23:40 
GeneralRe: My vote of 2 Pin
soni_mp24-Apr-15 1:12
soni_mp24-Apr-15 1:12 
GeneralRe: My vote of 7* Pin
Singh Vijay Kumar25-Apr-15 22:13
professionalSingh Vijay Kumar25-Apr-15 22:13 
QuestionRevision number differences? Pin
Trebor7418-Feb-13 5:44
Trebor7418-Feb-13 5:44 
AnswerRe: Revision number differences? Pin
StevenLJackson118-Feb-13 5:54
StevenLJackson118-Feb-13 5:54 
GeneralMy vote of 5 Pin
Yugan_SA10-Sep-12 22:22
Yugan_SA10-Sep-12 22:22 
GeneralMy vote of 5 Pin
Archimedes2413-Mar-12 6:02
professionalArchimedes2413-Mar-12 6:02 
QuestionOther Parameters Pin
Archimedes2410-Mar-12 0:32
professionalArchimedes2410-Mar-12 0:32 
AnswerRe: Other Parameters Pin
StevenLJackson112-Mar-12 12:19
StevenLJackson112-Mar-12 12:19 

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

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