Introduction
Hi. It's the first time that I am writing a CodeProject article. I feel honored. :) Anyway, Sometimes I'm too lazy to manually update the build number of my software every time a new build comes out. And I thought to find a similar tool and customize it to work as I wanted would be more complicated than writing my own. So here's the macro, to automatically update the build number in AssemblyInfo.cs for you. (For VB.NET projects this would be AssemblyInfo.vb) I kept it simple, so you can get the idea and do customizations yourself.
How does it work
Basically the macro, once triggered, tries to open the file AssemblyInfo.cs from a path you specify, and locates the version string (something like this in C#: [assembly:AssemblyVersion("1.0.0220.1536")]
) using regular expression. It then updates the old revision and build numbers based on some rules, and writes the file back. At the very end, an optional Build.BuildSolution
command is called. If you want to update the build number every time a new build process is kicked off, run this macro instead of using the normal Build->Build Solution menu item.
The rules of the calculation of build numbers are fairly simple. Normally a version number will look like this: AA.BB.CCDD.EEFF, where AA is the major version and BB is the minor version. These two are untouched: you have to set them yourself. The CCDD.EEFF part is modified automatically. CC will be used to represent the number of months elapsed since the starting of the project, and DD the number of days after those months. EE and FF will be the hours and minutes the build was created in that day. Therefore for example, if the project started on Jan 10, 2003 with version 1.0 and you run this macro on Mar 15, 2003 at 14:00, this is the build number that comes out of the calculation: 1.0.0205.1400. (I was told that this is the Microsoft's way of numbering the build.)
How to make it work
You will have to create a new macro with the source code included in this article. I assume you already know how to do that. Then, you will have to edit the source code to make it work for your project. The followings are what you need to change:
- Change variable
theProjectStartingDate
. This represents the starting date of your project, on which the calculation is based.
- Change variable
theAssemblyInfoFilePath
. The path is relative to the path of the project file (*.csproj for C#) I put all my source files under Source\ directory, so it's Source\AssemblyInfo.cs in the sample code. VB.NET users have to change the file name to AssemblyInfo.vb, and also the regular expression that parses the VB version string, correctly. I'm sorry but I'm not a VB programmer.
- Change variable
theBuildSolution
to False
if you don't want to build after the macro is done.
- Change the default version string from
1.0.*
to something like 1.0.0.0
so the macro can find it for the first time.
If you run this macro with AssemblyInfo.cs file open, you will be prompted whether to reload it. But the build process will not be interrupted.
Conclusion
This is it. It doesn't do much, and it may have problems handling multiple projects in one solution. But I think the code is comprehensive enough for other people to customize. Again, I'm not a VB programmer, so please don't laugh if you find this article childish. Thanx :)
History
- Sep 13 2003 - Initial release
- Sep 19 2003 - Source updated