Introduction
In VS 6, we had an autoincrement of the build number, if we build a new *.exe file. In VS.NET, I miss this feature. Here I submit a quick and dirty program (60 lines) which updates the version line in AssemblyInfo.cs each time a Release version is build.
Basics
Beau Scinner has written a lot about the basics, but it works only in VS 6. In VS.NET is AssemblyInfo.cs file, which contains the information of the Application.ProductVersion in the form of one line:
[assembly: AssemblyVersion("1.0.13")]
IncBuildNo reads this file, increments the last number by 1 and writes it back.
Calling IncBuildNo
IncBuildNo's 1st arg is the path and filename to AssemblyInfo.cs.
Optional it reads a 2nd arg Release. If the 2nd arg is given, but if arg!="Release", IncBuildNo returns without any action, I add this option, that IncBuildNo is able to select the VS.NET environment Release/Debug.
If you omit this 2nd arg or the 2nd arg is Release IncBuildNo increments the last number of the version string (otherwise it skips incrementing).
In the new version, I add some error-output to the Application Event Log.
Installation
If you download the project above, you have a complete installed example.
In your own projects, take the following steps:
- Copy IncBuildNo into the same directory as AssemblyInfo.cs (normally the
$(ProductDir))
- In VS.NET, select Project -> Properties -> Build Events
- Insert in Post-build Event CommandLine:
"$(ProjectDir)IncBuildNo" "$(ProjectDir)AssemblyInfo.cs" $(ConfigurationName)
- Select in Run the Post-Build Event?
When build updates the project output
Now each time you build a new release, the last number of the version is incremented.
1st Note: After a successful build of a release, AssemblyInfo.cs shows the version of the release +1.
2nd Note: If you use a installation-project when you build your application, you can insert a line in the postbuild event property in the project properties of this installation project. I use e.g.
"$(ProjectDir)IncBuildNo" "$(ProjectDir)..\AssemblyInfo.cs" $(ConfigurationName)
Attention: In this case, the $(ProjectDir) points to the install project, not to the application project. This is the reason for the ..\ phrase in the above line. But then the increment happened only if the building process is running, i.e., you can build a Release-Version without incrementing the build number.
I hope this will help you.
|
|
 |
 | My version of that PIEBALDconsult | 9:30 28 Jan '08 |
|
|
 |
|
 |
nice structured. Did you do some tests with Umlaute äöü? I had a lot of trouble with it. Hint: Add an entry in the eventlog and 'return with integers' eg. return -5. If there is an error in the postbuild-process you get only the integer as result.
Ariadne
|
|
|
|
 |
 | issues when implementing in c# SmartClient Project SirGolphknut | 9:24 4 Apr '06 |
|
 |
1) After the post build command args exec, I end up with a few high-ascii characters at the beginning of my AssemblyInfo.cs file, and before a couple other characters (the (c) char) I have to manually remove these characters.
2) it updates the AssemblyVersion number just fine, but when I reference "Application.ProductVersion" in my code, I get the AssemblyFileVersion version number.
Basically, I just need the version num to be incremented, AND accessible at runtime...
Any thoughts?
|
|
|
|
 |
|
 |
SirGolphknut wrote: I end up with a few high-ascii characters at the beginning of my AssemblyInfo.cs file
Workaround: 1. copy contents of AssemblyInfo.cs to Clipboard. 2. delete AssemblyInfo.cs from your project 3. add a new element to your project 4. select C#-class and name it AssemblyInfo.cs
5. Replace the default contents of the new class with contents of clipboard.
SirGolphknut wrote: when I reference "Application.ProductVersion" in my code, I get the AssemblyFileVersion version number.
I use Application.ProductVersion.ToString() too, and get the right build #. Are you shure, that you use the appropriate AssemblyInfo.cs?
Ariadne
modified on Tuesday, January 29, 2008 2:06:43 AM
|
|
|
|
 |
 | ... and this is how to do it in VB.NET !!! daniellus | 4:07 10 Nov '05 |
|
 |
Hi all,
Since there are also VB.NET users out there, but only C#.Net has `Build Events`, it seems that we guys and gals cant do this trick. Well, there is a work-a-round:
Download `PrePostBuildRules Add-in` from this page, that contains the "Visual Studio .NET 2003 Automation Samples":
http://www.microsoft.com/downloads/details.aspx?FamilyId=3FF9C915-30E5-430E-95B3-621DCCD25150&displaylang=en#filelist
Follow the instructions as in the Readme file, that is: (1) open and build this addin project, (2) close all VS.net instances (3) run the .reg file within the project dit
Then there will be added a `Solution Build Rules` to the Tools menu in VS.NET next time you open your project. So, open your project (that you want the autoincrement working on) and click:
Tools > Solution Build Rules > Pre-Build rules > (select your project) > Add new
Set program to: IncBuildNo.exe (download from this code project page) Set Command line to: (dont need to set this! I adjusted the code as seen below to always open AssemblyInfo.vb)
I replaced the code within the project with this one. The code supports the AssemblyInfo format used in VB.net. Also, I couldnt get command line arguments correctly passed. So, I deleted that piece of code and replaced it with the standard `AssemblyInfo.vb` string. So, you will only be able to work ith and use this filename. Else change this source or get this arg[] thing working (see original source code!)
It works fine for me:
using System; using System.IO; namespace IncBuildNo{ class IncBuildNo{ [STAThread] static int Main(string[] args){
#region local variable definitions StreamReader file; string sLine; string filename = "AssemblyInfo.vb"; string sFile=""; string[] sTmp; int iPos=0; #endregion
#region open and read cs file try {file = new StreamReader(filename,System.Text.Encoding.UTF7);} catch (Exception Ex){ Console.WriteLine("(0) File '"+args[0]+"' not found\nException thrown: "+Ex); return -1; } try{ while ((sLine = file.ReadLine()) != null){ #region interpret version line iPos=sLine.IndexOf("Assembly: AssemblyVersion("); if(iPos>0){ sTmp=sLine.Split('.'); if(sTmp[sTmp.Length-1]=="*\")>")sTmp[sTmp.Length-1]="0\")>"; int iTmp=int.Parse(sTmp[sTmp.Length-1].Substring(0,sTmp[sTmp.Length-1].Length-3))+1; sTmp[sTmp.Length-1]=iTmp.ToString(); sLine=""; for(int i=0;i"."; sLine=sLine.Substring(0,sLine.Length-1)+"\")>"; } #endregion sFile+=sLine+"\n"; //collect the rest } } catch(Exception ex){ Console.WriteLine("(1) (Reading File) Exception thrown: "+ex); return -1; } file.Close(); #endregion #region write cs file back try{ StreamWriter fileW = new StreamWriter(filename,false,System.Text.Encoding.UTF8); //write the file back fileW.Write(sFile.Substring(0,sFile.Length)); fileW.Close(); return 0; } catch (Exception Ex){ Console.WriteLine("(2) (Reading File) Exception thrown: "+Ex); return -1; } #endregion } } }
NOTE: in VS.NET 2005, Build Events will be integrated, just like C# via the properties menu!
|
|
|
|
 |
|
 |
Here is the actual VB.NET Code if desired:
Imports System.IO Imports System
Public Class BuildNumberIncrmenter
Public Shared Function Main(ByVal aArgs() As String) As Int32
Dim oFile As IO.File Dim oReader As StreamReader = Nothing Dim sLine As String = String.Empty Dim csFileName As String = "AssemblyInfo.vb" Dim sFile As String = String.Empty Dim aTmp() As String Dim iPos As Int32 = 0
' Try to open the file Try 'oFile = New StreamReader(csFileName, System.Text.Encoding.UTF7) oReader = oFile.OpenText(csFileName) Catch ex As Exception Console.WriteLine("(0) File '" & csFileName & "' not found" & vbCrLf & "Exception thrown: " & ex.Message) Return -1 End Try
Try
' Get and read all lines While oReader.Peek <> -1 sLine = oReader.ReadLine()
' This is the ONLY line we want to change iPos = sLine.IndexOf("Assembly: AssemblyVersion(") If (iPos > 0) Then
' Get the text between '.' separated in an array aTmp = sLine.Split(".".ToCharArray)
' Add a 0 if the last set is an asterisk If aTmp(aTmp.Length - 1).Trim = "*"")>".Trim Then aTmp(aTmp.Length - 1) = "0"")>" End If
' Increment the last number by 1 Dim iTmp As Int32 = CInt(aTmp(aTmp.Length - 1).Substring(0, aTmp(aTmp.Length - 1).Length - 3)) + 1
aTmp(aTmp.Length - 1) = CStr(iTmp) sLine = String.Empty
' Concatenate the string again For i As Int32 = 0 To aTmp.Length - 1 sLine &= aTmp(i) & "." Next sLine = sLine.Substring(0, sLine.Length - 1) & """)]"
End If ' If (iPos > 0) Then
' Get the rest of the line sFile &= sLine & vbCrLf
End While
oReader.Close()
Catch ex As Exception Console.WriteLine("(1) (Reading File) Exception thrown: " & ex.Message) Return -1 End Try
Try
' Write the file back Dim oFileW As New StreamWriter(csFileName, False, System.Text.Encoding.UTF8) With oFileW .WriteLine(sFile.Substring(0, sFile.Length)) .Close() End With Return 0
Catch ex As Exception Console.WriteLine("(2) (Writing File) Exception thrown: " & ex.Message) Return -1 End Try
End Function
End Class
|
|
|
|
 |
 | Increment build number in VS.Net Koszyk | 1:00 3 Aug '05 |
|
 |
This is possible to use * in AssemblyInfo.cs. You can read: " You do not have to set and update each part explicitly because you can use wild characters (*) to automatically generate the build and revision numbers. Visual Studio .NET generates an AssemblyInfo source file with the AssemblyVersion attribute defined as follows:
[assembly: AssemblyVersion("1.0.*")] The result of this is a build number set to the number of days since a random, designated start date and the revision based on the number of seconds since midnight.
You can either use auto-increment version numbers or opt to manually control version numbers as part of the build process. Each approach has associated benefits and drawbacks. " in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/tdlg_ch5.asp[^] The problem is that in some cases this is better to control version number manually. However there is a number of build tools (relatively cheap), which help you with such type of task eg. FinalBuilder or Visual Build Professional. In VS 2005 will be introduced also some new build tool.
------------------------- Adam Koszlajda
|
|
|
|
 |
|
 |
In the same Microsoft Doc you see:
Disadvantages
Using auto-increment version numbers has the following disadvantages:
- The internal assembly build number does not match your system build number, which means there is no easy way to correlate a particular assembly with the build that generated it. This may be particularly problematic when you need to support your system in a production environment.
- Build and revision numbers are not increased by one but are based on the time an assembly is built.
- A new version of an assembly is generated each time it is built regardless of whether any changes have been made to the assembly. For strongly named assemblies, this means that all clients of that assembly must also be rebuilt to point to the correct version. However, if the build process rebuilds the whole system this should not be an issue.
Even the 2nd point was the reason to write IncBuildNo
Ariadne
|
|
|
|
 |
|
|
Last Updated 28 Jan 2008 |
Advertise |
Privacy |
Terms of Use |
Copyright ©
CodeProject, 1999-2010