I have the following in
BuildVersion.cs
which is a
linked file in the solution folder of the projects :
using System.Reflection;
[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 :
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 + "\"");
StreamWriter sw = new StreamWriter(args[0]);
sw.Write(s);
sw.Close();
}
}
}