Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Automatically Increment Build Numbers in Visual Studio 2005

0.00/5 (No votes)
19 Apr 2006 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here