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

Increment Build Number in VS.NET

, 28 Jan 2008 CPOL
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

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:


Comments and Discussions

Suggestion2 suggestions PinmemberLeon Zeng1 hr 36mins ago 
GeneralMy version of that PinmemberPIEBALDconsult28-Jan-08 9:30 
GeneralRe: My version of that PinmemberAriadne28-Jan-08 21:22 
Generalissues when implementing in c# SmartClient Project PinmemberSirGolphknut4-Apr-06 9:24 
GeneralRe: issues when implementing in c# SmartClient Project [modified] PinmemberAriadne28-Jan-08 3:46 
General... and this is how to do it in VB.NET !!! Pinmemberdaniellus10-Nov-05 4:07 
GeneralRe: ... and this is how to do it in VB.NET !!! Pinmemberimxuf23-May-07 13:58 
Here is the actual VB.NET Code if desired:
Imports System.IO
Imports System
Public Class BuildNumberIncrmenter
Public Shared Function Main(ByVal aArgs() As String) As Int32
Dim oFile As IO.File
Dim oReader As StreamReader = Nothing
Dim sLine As String = String.Empty
Dim csFileName As String = "AssemblyInfo.vb"
Dim sFile As String = String.Empty
Dim aTmp() As String
Dim iPos As Int32 = 0
' Try to open the file
'oFile = New StreamReader(csFileName, System.Text.Encoding.UTF7)
oReader = oFile.OpenText(csFileName)
Catch ex As Exception
Console.WriteLine("(0) File '" & csFileName & "' not found" & vbCrLf & "Exception thrown: " & ex.Message)
Return -1
End Try
' Get and read all lines
While oReader.Peek <> -1
sLine = oReader.ReadLine()
' This is the ONLY line we want to change
iPos = sLine.IndexOf("Assembly: AssemblyVersion(")
If (iPos > 0) Then
' Get the text between '.' separated in an array
aTmp = sLine.Split(".".ToCharArray)
' Add a 0 if the last set is an asterisk
If aTmp(aTmp.Length - 1).Trim = "*"")>".Trim Then
aTmp(aTmp.Length - 1) = "0"")>"
End If
' Increment the last number by 1
Dim iTmp As Int32 = CInt(aTmp(aTmp.Length - 1).Substring(0, aTmp(aTmp.Length - 1).Length - 3)) + 1
aTmp(aTmp.Length - 1) = CStr(iTmp)
sLine = String.Empty
' Concatenate the string again
For i As Int32 = 0 To aTmp.Length - 1
sLine &= aTmp(i) & "."
sLine = sLine.Substring(0, sLine.Length - 1) & """)]"
End If ' If (iPos > 0) Then
' Get the rest of the line
sFile &= sLine & vbCrLf
End While
Catch ex As Exception
Console.WriteLine("(1) (Reading File) Exception thrown: " & ex.Message)
Return -1
End Try
' Write the file back
Dim oFileW As New StreamWriter(csFileName, False, System.Text.Encoding.UTF8)
With oFileW
.WriteLine(sFile.Substring(0, sFile.Length))
End With
Return 0
Catch ex As Exception
Console.WriteLine("(2) (Writing File) Exception thrown: " & ex.Message)
Return -1
End Try
End Function
End Class

GeneralIncrement build number in VS.Net PinmemberKoszyk3-Aug-05 1:00 
GeneralRe: Increment build number in VS.Net PinmemberAriadne3-Aug-05 4:53 

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.141223.1 | Last Updated 28 Jan 2008
Article Copyright 2005 by Ariadne
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid