Click here to Skip to main content
13,193,600 members (42,808 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 29 Jul 2005

Increment Build Number in VS.NET

, 28 Jan 2008
Rate this:
Please Sign up or sign in to vote.
Little quick and dirty program (60 lines) which updates the version build number each time a release is built.


In VS 6, we had an autoincrement of the build number, if we build a new *.exe file. In VS.NET, I miss this feature. Here I submit a quick and dirty program (60 lines) which updates the version line in AssemblyInfo.cs each time a Release version is build.


Beau Scinner has written a lot about the basics, but it works only in VS 6. In VS.NET is AssemblyInfo.cs file, which contains the information of the Application.ProductVersion in the form of one line:

// Version information for an assembly consists of the following four values:
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.13")]

IncBuildNo reads this file, increments the last number by 1 and writes it back.

Calling IncBuildNo

IncBuildNo's 1st arg is the path and filename to AssemblyInfo.cs.
Optional it reads a 2nd arg Release. If the 2nd arg is given, but if arg!="Release", IncBuildNo returns without any action, I add this option, that IncBuildNo is able to select the VS.NET environment Release/Debug.
If you omit this 2nd arg or the 2nd arg is Release IncBuildNo increments the last number of the version string (otherwise it skips incrementing).

In the new version, I add some error-output to the Application Event Log.


If you download the project above, you have a complete installed example.

In your own projects, take the following steps:

  1. Copy IncBuildNo into the same directory as AssemblyInfo.cs (normally the $(ProductDir))
  2. In VS.NET, select Project -> Properties -> Build Events
  3. Insert in Post-build Event CommandLine:
    "$(ProjectDir)IncBuildNo" "$(ProjectDir)AssemblyInfo.cs" $(ConfigurationName)
  4. Select in Run the Post-Build Event? When build updates the project output

Now each time you build a new release, the last number of the version is incremented.

1st Note: After a successful build of a release, AssemblyInfo.cs shows the version of the release +1.

2nd Note: If you use a installation-project when you build your application, you can insert a line in the postbuild event property in the project properties of this installation project. I use e.g.

"$(ProjectDir)IncBuildNo" "$(ProjectDir)..\AssemblyInfo.cs" $(ConfigurationName) 

Attention: In this case, the $(ProjectDir) points to the install project, not to the application project. This is the reason for the ..\ phrase in the above line. But then the increment happened only if the building process is running, i.e., you can build a Release-Version without incrementing the build number.

I hope this will help you.


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


About the Author

Software Developer (Senior) EndoTherm GmbH
Switzerland Switzerland
My first approach to computers was in 1970 a IBM1160, a real desk computer, this means it was as large as a desk and had a cartreader and 32 kByte Memory! But it runs FORTRAN! Ok 'run' is a little too fast <sm>

Later I was very proud on my Comodore CBM8032 32KbRAM+32KbRom (uff!) and 2 (in words 'TWO') floppies. But it was able to run a Basic-Interpreter. (There was a young gay in USA, who invent and developed the garbage collecter for such interpreter, his name was Bill G.)

Unfortunatly this CBM8032 burned away due to some "experiements"...

Ok, later I studied physics and work now since 20 years in the cancer research in Switzerland. My own little company Ariadne-Consulting delivers the login name:


You may also be interested in...


Comments and Discussions

Suggestion2 suggestions Pin
Leon Zeng27-Dec-14 16:42
memberLeon Zeng27-Dec-14 16:42 
GeneralMy version of that Pin
PIEBALDconsult28-Jan-08 8:30
memberPIEBALDconsult28-Jan-08 8:30 
GeneralRe: My version of that Pin
Ariadne28-Jan-08 20:22
memberAriadne28-Jan-08 20:22 
Generalissues when implementing in c# SmartClient Project Pin
SirGolphknut4-Apr-06 8:24
memberSirGolphknut4-Apr-06 8:24 
GeneralRe: issues when implementing in c# SmartClient Project [modified] Pin
Ariadne28-Jan-08 2:46
memberAriadne28-Jan-08 2:46 
General... and this is how to do it in VB.NET !!! Pin
daniellus10-Nov-05 3:07
memberdaniellus10-Nov-05 3:07 
Hi all,

Since there are also VB.NET users out there, but only C#.Net has `Build Events`, it seems that we guys and gals cant do this trick. Well, there is a work-a-round:

Download `PrePostBuildRules Add-in` from this page, that contains the "Visual Studio .NET 2003 Automation Samples":

Follow the instructions as in the Readme file, that is:
(1) open and build this addin project,
(2) close all instances
(3) run the .reg file within the project dit

Then there will be added a `Solution Build Rules` to the Tools menu in VS.NET next time you open your project.
So, open your project (that you want the autoincrement working on) and click:

Tools > Solution Build Rules > Pre-Build rules > (select your project) > Add new

Set program to: IncBuildNo.exe (download from this code project page)
Set Command line to: (dont need to set this! I adjusted the code as seen below to always open AssemblyInfo.vb)

I replaced the code within the project with this one. The code supports the AssemblyInfo format used in Also, I couldnt get command line arguments correctly passed. So, I deleted that piece of code and replaced it with the standard `AssemblyInfo.vb` string.
So, you will only be able to work ith and use this filename. Else change this source or get this arg[] thing working (see original source code!)

It works fine for me:

<br />
using System;<br />
using System.IO;<br />
namespace IncBuildNo{<br />
	class IncBuildNo{<br />
		[STAThread]<br />
		static int Main(string[] args){<br />
<br />
#region local variable definitions<br />
			StreamReader file;<br />
			string sLine;<br />
			string filename = "AssemblyInfo.vb";<br />
			string sFile="";<br />
			string[] sTmp;<br />
			int iPos=0;<br />
#endregion<br />
			<br />
#region open and read cs file<br />
			try {file = new StreamReader(filename,System.Text.Encoding.UTF7);}	//try to open the file<br />
			catch (Exception Ex){<br />
				Console.WriteLine("(0) File '"+args[0]+"' not found\nException thrown: "+Ex);<br />
				return -1;<br />
			}<br />
			try{			//ge and read all lines<br />
				while ((sLine = file.ReadLine()) != null){<br />
					#region interpret version line<br />
					iPos=sLine.IndexOf("Assembly: AssemblyVersion(");	//this line we want to change<br />
					if(iPos>0){<br />
						sTmp=sLine.Split('.');							//get the text between '.' separated in an array<br />
						if(sTmp[sTmp.Length-1]=="*\")>")sTmp[sTmp.Length-1]="0\")>";<br />
						int iTmp=int.Parse(sTmp[sTmp.Length-1].Substring(0,sTmp[sTmp.Length-1].Length-3))+1; //increment the last number by 1<br />
						sTmp[sTmp.Length-1]=iTmp.ToString();<br />
						sLine="";<br />
						for(int i=0;i<sTmp.Length;i++)sLine+=sTmp[i]+".";//concatenate the string again<br />
						sLine=sLine.Substring(0,sLine.Length-1)+"\")>";<br />
					}<br />
#endregion<br />
					sFile+=sLine+"\n";			//collect the rest<br />
				}<br />
			}<br />
			catch(Exception ex){<br />
				Console.WriteLine("(1) (Reading File) Exception thrown: "+ex);<br />
				return -1;<br />
			}<br />
			file.Close();<br />
			#endregion<br />
			#region write cs file back<br />
			try{<br />
				StreamWriter fileW = new StreamWriter(filename,false,System.Text.Encoding.UTF8); //write the file back<br />
				fileW.Write(sFile.Substring(0,sFile.Length));<br />
				fileW.Close();<br />
				return 0;<br />
			}<br />
			catch (Exception Ex){<br />
				Console.WriteLine("(2) (Reading File) Exception thrown: "+Ex);<br />
				return -1;<br />
			}<br />
			#endregion<br />
		}<br />
	}<br />
}	<br />

NOTE: in VS.NET 2005, Build Events will be integrated, just like C# via the properties menu!
GeneralRe: ... and this is how to do it in VB.NET !!! Pin
imxuf23-May-07 12:58
memberimxuf23-May-07 12:58 
GeneralIncrement build number in VS.Net Pin
Koszyk3-Aug-05 0:00
memberKoszyk3-Aug-05 0:00 
GeneralRe: Increment build number in VS.Net Pin
Ariadne3-Aug-05 3:53
memberAriadne3-Aug-05 3:53 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171018.2 | Last Updated 28 Jan 2008
Article Copyright 2005 by Ariadne
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid