Visual Studio: Auto-increment Build Number – Part 2/3

Visual Studio: Auto-increment Build Number – Part 2/3

Part 1

Concept || Real

I am using the setup, we are discussing here, for last 8000+ builds I compiled on my dev box with Visual Studio. I can tell you that this is a robust code and pretty smoothly working setup.

What & How?

We will leverage powerful post-build event + PowerShell script (full script is discussed below) to have Visual Studio build process do what we want. We will do following:

  • Write aPowerShell script which does following tasks:
      Pick just-compiled dll from calling project’s folder
      Read File Version field
      Increment Build number field in the version by 1
      Write the new Version number with incremented Build number to AssemblyInfo.cs of the calling project
  • Write a wrapper batch file to call aforesaid PowerShell script
  • Configure project to call the batch file with appropriate params
  • Let Visual Studio know when to run the post-build event

After every build we will have our read-only BuildLog.txt file in project folder updated, like so:


Note: The highlighted header is manually inserted, script does not write the header automatically. If you want header in your BuildLog.txt, edit it manually once and insert header at the top.

So, the conceptual diagram of setup looks like this:


The Script

I wrote this PowerShell script using NotePad++. You will find script very similar to c# because PowerShell utilizes a lot of .NET.  If you wish to edit the script to extend its functionality, you can use any editor of your choice, e.g. PowerShell GUI Editor, which provides excellent debug support.
We will call our PowerShell script file UpdateBuildLog.ps1. Here is the outer structure of our script:


  1. File Header
  2. We will wrap our script in try catch block so as to ensure we know the reason of the script failure in our build output windows. We will see how.
  3. Parameter:projectPath – Path of the project, defaulted to c:\temp\
  4. Parameter:targetDLLFullPath – Where project’s dll is located, defaulted to c:\temp\
  5. Parameter:extraAttribute – Any extra text attribute for build log entry, defaulted to XXX
  6. Parameter:buildConfig – Build configuration, Debug|Release, defaulted to Debug

Let’s have a look at the internals of the script:


  1. Prepare AssemblyInfo.cs path from supplied project location. Write the prepared information to Project’s build output window.
  2. Read just created dll file from the supplied path and file name, and extract version and build number. Write version and build number to build output window. Increment build number by 1 in the extracted version number.
  3. Prepare Build log entry, write the entry into build log text file, make the build log text file readonly again.
  4. Update the AssemblyInfo file with the new version number.
  5. Confirm proper version number is written to AssemblyInfo file and write to build output window.
  6. If there is any issue while updating new version number, write to build output window.

The Batch

We will call our batch file PostBuild.bat.  We will setup our project’s post build event in Visual Studio to call PostBuild.bat with appropriate parameters. Following are important parts of the batch file:


  1. File Header
  2. Prepare parameters received from Visual Studio build events
  3. Call procedures on the basis of supplied switches
  4. Customize the batch file and write your code (by default, this section does nothing)
  5. Customize the batch file and write your code (by default, this section does nothing)
  6. Procedure to call PowerShell script to update build log with incremental build number.

We now have a decent idea of the nuts and bolts, and we  are clear about the structure of the setup, script and the batch file.

Code download and customization in – Part 3

Hope you are still with me.

4 thoughts on “Visual Studio: Auto-increment Build Number – Part 2/3

  1. Pingback: Auto-increment Build Number – Part 1/3 | Sharp Statements

  2. Pingback: Auto-increment Build Number-Part 3/3 | Sharp Statements

  3. It’s pretty weak to use PowerShell and BAT files for this, when all you need to do is write a very small C# program that can do all of this quickly and easily from a Pre or Post build event in any project.

    • Thanks for reading and commenting. I couldn’t get why you are calling script+batch a weak solution. Regarding c# part of your comment, PowerShell uses .NET so it’s using the same framework what c# would have used, and I think a c# program would have same number of lines as PS script has, though I am not sure. Yes this could be done using c# but I chose scripting + batch for two reasons – manageability and quick and easy plugging ability. For example in my case, other than auto-increment command, I have two other third party post-built commands to be run, conditionally. I run these two post-build commands in batch file, and then call auto-increment script. If I need to add/remove a command, I can easily do so in the batch file, no compilation/executable changes required.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s