Click here to Skip to main content
Licence CPOL
First Posted 19 Apr 2006
Views 53,121
Downloads 322
Bookmarked 24 times

Automatically Increment Build Numbers in Visual Studio 2005

By | 19 Apr 2006 | Article
A demonstration of how to harness the power of the DTE and macros in Visual Studio, to create a build number incrementer.

Introduction

This macro automatically increments the build number of a project by one, every time a build is started.

Background

One of my long time gripes about Visual Studio is the lack of an option to just increment the build number every time I hit the Build button. Sure, this is not the most elegant solution, but it's better than nothing. When I was originally contemplating this project, I was looking for a way to intelligently check whether the increment is warranted or not, like say in C#, I hit the Build button just to check for errors, so I might want the build to increment every time I hit F5 instead. However, there is no good way to do this using the interfaces provided by Visual Studio, so we will just have to make one.

Using the code

Download the code and extract it. Open up Visual Studio, and from the Tools menu, select Macros->Macros IDE. Under the MyMacros heading in the Macro Project Explorer, open EnvironmentEvents and copy the function from the corresponding file in the Zip. Repeat for the functions in Module1.vb in the Zip. That's all, your project build number will now be automatically incremented. The code is very heavily commented, so if you have any questions, the comments are bound to answer them.

Points of Interest

One interesting note that tripped me up is that VB sticks a comment demonstrating how to set a wildcard for the AssemblyVersion attribute. As the comment came before the actual code line, the macro would read it first and then would be unable to find the AssemblyFileVersion attribute and would error out. My solution was to have the macro check for this commented line and remove it every time the macro was run. It is a brute-force solution, but it solved the problem.

There is currently no way to turn this macro off unless you erase the event handler in EnvironmentEvents.

If you have a wildcard character (*) in the build number field, it will be automatically replaced with a 1 when the build is started.

Currently, the macro only operates on C# and Visual Basic projects, and ignores everything else. However, it would be very easy to expand the function to include your own language of choice.

To my knowledge, C++ is so different in structure from .NET that this incrementer will not work on pure C++ projects.

History

  • 1.0 - First release.

License

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

About the Author

Bellerephon

Team Leader
Prospective Software Inc.
United States United States

Member

My name is Adam Wilson.
I am business software developer with over 7 years of VB.NET experience, starting with Visual Studio 2002 RC1. However I made the switch to C# a few years ago and I haven't looked back. I started programming in VB6 13 years ago, and have been on employed as a Senior Software Developer/IT Manager at a small ISV since 2003.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNumber updated too late PinmemberLuke Edwards0:34 3 Feb '09  
GeneralGoodness Gracious [modified] Pinmembertoddmo19:07 16 Sep '08  
'Here ya go. This finally got it done for me...
'thanks to the author for a good starting point.

Option Strict Off
Option Explicit Off
 
Imports System
Imports System.IO
Imports EnvDTE
Imports System.Diagnostics
Imports System.Text.RegularExpressions
 
Public Module EnvironmentEvents
 
#Region "Automatically generated code, do not modify"
    'Automatically generated code, do not modify
    'Event Sources Begin
    <System.ContextStaticAttribute()> Public WithEvents DTEEvents As EnvDTE.DTEEvents
    <System.ContextStaticAttribute()> Public WithEvents DocumentEvents As EnvDTE.DocumentEvents
    <System.ContextStaticAttribute()> Public WithEvents WindowEvents As EnvDTE.WindowEvents
    <System.ContextStaticAttribute()> Public WithEvents TaskListEvents As EnvDTE.TaskListEvents
    <System.ContextStaticAttribute()> Public WithEvents FindEvents As EnvDTE.FindEvents
    <System.ContextStaticAttribute()> Public WithEvents OutputWindowEvents As EnvDTE.OutputWindowEvents
    <System.ContextStaticAttribute()> Public WithEvents SelectionEvents As EnvDTE.SelectionEvents
    <System.ContextStaticAttribute()> Public WithEvents BuildEvents As EnvDTE.BuildEvents
    <System.ContextStaticAttribute()> Public WithEvents SolutionEvents As EnvDTE.SolutionEvents
    <System.ContextStaticAttribute()> Public WithEvents SolutionItemsEvents As EnvDTE.ProjectItemsEvents
    <System.ContextStaticAttribute()> Public WithEvents MiscFilesEvents As EnvDTE.ProjectItemsEvents
    <System.ContextStaticAttribute()> Public WithEvents DebuggerEvents As EnvDTE.DebuggerEvents
    <System.ContextStaticAttribute()> Public WithEvents ProjectsEvents As EnvDTE.ProjectsEvents
    'Event Sources End
    'End of automatically generated code
#End Region
 

    Sub WriteToOutputBuildPane(ByVal Message As String)
        DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object.OutputWindowPanes.Item("Build").OutputString(Message & vbCrLf)
    End Sub
 
    Public Function NewAssemblyVersion(ByVal AssemblyVersion As String) As String
        Dim AssemblyVersionParts() As String = AssemblyVersion.Split(".")
        Dim BuildNumberPart As String = AssemblyVersionParts(2)
        Dim BuildNumber As Integer
        If BuildNumberPart = "*" Then
            BuildNumber = 1
        Else
            BuildNumber = CInt(BuildNumberPart) + 1
        End If
        AssemblyVersionParts(2) = BuildNumber
        Return String.Join(".", AssemblyVersionParts)
 
    End Function
 
    Private Sub BuildEvents_OnBuildProjConfigBegin(ByVal Project As String, ByVal ProjectConfig As String, ByVal Platform As String, ByVal SolutionConfig As String) Handles BuildEvents.OnBuildProjConfigBegin
        Dim AssemblyInfoFileName As String
 
        Select Case True
            Case Project.ToLower.EndsWith("vbproj")
                AssemblyInfoFileName = IO.Path.GetDirectoryName(IO.Path.GetDirectoryName(DTE.Solution.FullName) & "\" & Project) & "\AssemblyInfo.vb"
            Case Project.ToLower.EndsWith("csproj")
                AssemblyInfoFileName = IO.Path.GetDirectoryName(IO.Path.GetDirectoryName(DTE.Solution.FullName) & "\" & Project) & "\AssemblyInfo.cs"
        End Select
 
        Dim AssemblyInfoText As String
 
        If File.Exists(AssemblyInfoFileName) Then
 
            Dim StreamReader As StreamReader
            Try
                StreamReader = New StreamReader(AssemblyInfoFileName)
                AssemblyInfoText = StreamReader.ReadToEnd()
            Catch ex As Exception
                WriteToOutputBuildPane("Unable to read from " & AssemblyInfoFileName)
            Finally
                StreamReader.Close()
            End Try
 
            Dim AssemblyVersion As String = Regex.Match(AssemblyInfoText, "\<Assembly\: AssemblyVersion\(""(?'assemblyversion'.+?)""\)\>").Groups("assemblyversion").Value 'Extract the AssemblyVersion attribute value
            Dim NewAssemblyVersion As String = EnvironmentEvents.NewAssemblyVersion(AssemblyVersion)
 
            '' Write the newly modified AssemblyInfo file.
            Dim StreamWriter As StreamWriter
            Try
                StreamWriter = File.CreateText(AssemblyInfoFileName)
                StreamWriter.Write(AssemblyInfoText.Replace(AssemblyVersion, NewAssemblyVersion))
                WriteToOutputBuildPane("Assembly Version: " & NewAssemblyVersion)
            Catch ex As Exception
                WriteToOutputBuildPane("Unable to write to the AssemblyInfo file, the build number has not been changed!") 'For whatever reason we were not allowed to write to the file.
            Finally
                StreamWriter.Close()
            End Try
 
        Else
            WriteToOutputBuildPane("Unable to find " & AssemblyInfoFileName)  'We were unable to locate the AssemblyInfo file
        End If
 
    End Sub
End Module

 
Thanks,
toddmo
modified on Wednesday, September 17, 2008 1:24 AM

GeneralThe event BuildEvents_OnBuildProjConfigBegin doesn't fire PinmemberGiGi Abbrescia0:02 28 May '07  
GeneralFeature exists in VS2005 Pinmembersimabo1:04 5 Mar '07  
GeneralRe: Feature exists in VS2005 Pinmembermikey2226:21 5 Jun '07  
GeneralRe: Feature exists in VS2005 (Works in VS2008 Express as well) PinmemberTyreck9:52 19 Jan '09  
GeneralRe: Feature exists in VS2005 PinmemberAveryH15:15 9 May '09  
QuestionIncrement a Solution dosn't work?! Pinmemberj5136p11:34 8 Nov '06  
QuestionRe: Increment a Solution dosn't work?! PinmemberBellerephon7:07 8 Nov '06  
QuestionRe: Increment a Solution dosn't work?! Pinmemberj5136p118:48 8 Nov '06  
GeneralNot working for me... Pinmemberdirigo2:14 14 May '06  
AnswerRe: Not working for me... PinmemberBellerephon21:54 15 May '06  
GeneralRe: Not working for me... Pinmemberdirigo5:17 16 May '06  
AnswerRe: Not working for me... PinmemberBellerephon20:29 17 May '06  
QuestionNot in Express versions PinmemberSLURPO3:14 26 Apr '06  
AnswerRe: Not in Express versions PinmemberBellerephon18:25 26 Apr '06  
GeneralAutoincrementing build numbers is considered a bug in VS.NET 2003!!! Pinmemberperformer240:21 26 Apr '06  
AnswerRe: Autoincrementing build numbers is considered a bug in VS.NET 2003!!! PinmemberBellerephon18:28 26 Apr '06  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120604.1 | Last Updated 19 Apr 2006
Article Copyright 2006 by Bellerephon
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid