Eddie Garmon's Weblog

some architecture, some c#

Auto-Incrementing Build Numbers (C# for VS.Net)

I finally got fed up with manually updating the build numbers in the AssemblyInfo.cs files. I needed an automated solution, so now I have one, even though it is definately more of a hack than good code.

To the point, I created a simple console app that I placed in my bin directory, and use a Pre-BuildEvent to call. (i.e. BuildNumberIncrementer “Path to a\AssemblyInfo.cs”).

The hack that is BuildNumberIncrementer goes like :

 

   1:  using System;
   2:  using System.IO;
   3:  using System.Text.RegularExpressions;
   4:   
   5:  namespace BuildNumberIncrementer {
   6:      class EntryPoint {
   7:          [STAThread]
   8:          static void Main(string[] args) {
   9:              if (args.Length != 1) {
  10:                  Console.WriteLine("Please specify the 'AssemblyInfo.cs' you want to update.");
  11:                  return;
  12:              }
  13:              string infoPath = args[0];
  14:   
  15:              if (!infoPath.EndsWith("AssemblyInfo.cs")) {
  16:                  Console.WriteLine("Please specify the 'AssemblyInfo.cs' you want to update.");
  17:                  return;
  18:              }
  19:   
  20:              if (!File.Exists(infoPath)) {
  21:                  Console.WriteLine(infoPath + " does not exist.");
  22:                  return;
  23:              }
  24:   
  25:              StreamReader reader = File.OpenText(infoPath);
  26:              string contents = reader.ReadToEnd();
  27:              reader.Close();
  28:              Regex version = new Regex(@"(\d+\.\d+\.\d+\.)(\d+)");
  29:              Match versionMatch = version.Match(contents);
  30:              string oldVersion = versionMatch.Value;
  31:              string newVersion = versionMatch.Groups[1].Value + (Convert.ToUInt32(versionMatch.Groups[2].Value) + 1).ToString();
  32:              contents = contents.Replace(oldVersion, newVersion);
  33:              StreamWriter writer = File.CreateText(infoPath);
  34:              writer.Write(contents);
  35:              writer.Close();
  36:   
  37:              Console.WriteLine(string.Format("New version is [{0}]", newVersion));
  38:          }
  39:      }
  40:  }

Comments

Jim said:

I put together a macro, a while ago, that increments the build number in an AssemblyInfo.cs. You can view it here: http://blogs.biasecurities.com/jim/posts/166.aspx
# December 3, 2003 8:14 PM

Andrew said:

What don't you like, or what doesn't work for you with the default generated by VS?

//
// 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.*")]
# December 3, 2003 8:24 PM

Eddie Garmon said:

Anderw,
I plan to use this in an automated build environment outside of VS.Net eventually, and the VS versioning by * is not incremental.
# December 3, 2003 8:42 PM

Paul Wilson said:

Sounds familiar. My automated build includes a very similar console app for my VB projects.
# December 3, 2003 8:47 PM

Darrell said:

What about, say, BuildIt or Hippo.NET? Both of those include auto-incrementing capabilities.
# December 4, 2003 7:59 AM

Andrew said:

Ahh ok.

1.0.1433.22226
1.0.1433.22242
1.0.1433.22273

This is kind of silly, it jumps by large segments 8| even though nothing changed.

As long as it gets bigger it still counts as incremental :P
# December 4, 2003 12:34 PM

Eddie Garmon said:

yeah the current scheme (*) supported in VS is based on the system time at compilation. There are other factors in there, but the time factor, I have found, is the biggest in relation to the generated build #.
# December 4, 2003 1:19 PM

John Lyon-Smith said:

If anyone's interested I've put together a more robust solution for solving this problem at http://www.lyon-smith.org/downloads.aspx#vrt. There's also link to a CodeProject article that I wrote on how to use the MkVer tool.
# January 9, 2004 10:55 AM

3f blog » Blog Archive » Build number en .Net said:

Pingback from  3f blog  » Blog Archive   » Build number en .Net

# February 28, 2008 8:48 AM

Good day! Veri nice 7444 site. See more <a href=" http://potato.com "> potates </a>, this is my 125 new 6954 site. said:

Good 274 day! 457 Veri nice site. 958 See more 9864 <a href=" http://potato.com "> 746 potates 6336 </a>, this 757 is my 08 new site.

# August 30, 2009 12:55 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)