Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to Automatically Version Assemblies in C#.Net.

I have a requirement as follows:
1. In my solution I have multiple DLLs and EXE which are built and deployed.
2. I would like to version them all with same version numbers and it should increment automatically when I build my solution in Release mode, either manual build or auto build.


what I need to achieve is it should automatically increment X.X.*.X where * is the build number.

Please let me know if anyone have encountered this situation before and can help.

Thank you

What I have tried:

I have tried below things but no help:

1. Modified AssemblyInfo.cs file by providing '*' in place of zero's in version number like "1.0.*"
2. Followed steps in this article, The Right Way to Version Your Assemblies[^]
Posted
Updated 26-Sep-16 0:22am

1 solution

I have the following in BuildVersion.cs which is a linked file in the solution folder of the projects :
C#
using System.Reflection;
// build number = 368
// build version = 3.3.14

[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.3.14.368")]

With the following line in one of the projects pre build event :
"$(SolutionDir)tools\buildversion.exe" "$(SolutionDir)buildversion.cs"

The build number will be incremented on each build, the version numbers you can change by hand when needed, AssemblyVersion and AssemblyFileVersion are different so if you use the GAC it will overwrite for non breaking changes but you still get the file version if you view it in Explorer.
And the increment is done with the following code which is compiled to an exe :
C#
using System;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace pp
{
	public class pp
	{
		public static void Main(string[] args)
		{
			if(args.Length<1) return;
			
			string filename = args[0];
			string buildversion = "";
			int buildnum = 0;
			
			
			StreamReader sr = new StreamReader(filename);
			
			string s = sr.ReadToEnd();
			sr.Close();
			string buildnumregex = @"\s*//\s*build\s*number\s*=\s*(?<num>\w*)";
			string buildverregex = @"\s*//\s*build\s*version\s*=\s*(?<vers>[\w.]*)";
			Regex mr = new Regex(
				buildnumregex,
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			Match   m = mr.Match(s);
			string ss= m.Groups["num"].Value;
			if(ss !="") 
			{
				buildnum = int.Parse(ss);
				buildnum++;
				s=mr.Replace(s,"\r\n// build number = "+buildnum.ToString());
			}
			
			mr = new Regex(
				buildverregex,
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			m = mr.Match(s);
			ss =m.Groups["vers"].Value;
			if(ss !="") buildversion = ss;
			
			string currbuild = buildversion+"."+buildnum.ToString();
			
			mr = new Regex(
				@"AssemblyFileVersion\s*\(""(?<ver>[\w.]*"")",
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			m=mr.Match(s);
			s=mr.Replace(s,"AssemblyFileVersion(\""+ currbuild + "\"");

			//MessageBox.Show(s);
			
			StreamWriter sw = new StreamWriter(args[0]);
			sw.Write(s);
			sw.Close();
		}
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900