in

ASP.NET Weblogs

Eddie Garmon's Weblog

some architecture, some c#

December 2003 - Posts

  • 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:  }
More Posts