Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Incrementing AssemblyVersion revision number on each build

, 3 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Some people remarked that the Visual-Studio-generated build number actually contains a hidden timestamp, which, I think, is far from being intuitive. I wrote the following snippet to do something similar but is easier to understand.Beside changing the version number, we can even write build...
Some people remarked that the Visual-Studio-generated build number actually contains a hidden timestamp, which, I think, is far from being intuitive. I wrote the following snippet to do something similar but is easier to understand.
 
Beside changing the version number, we can even write build date/time into the AssemblyDescription attribute. Thus it is very easy to figure out the build date when we review the properties of the assembly file.
 
The following code requires an AssemblyDescription attribute with the date format (and an optional time format, which will be overwritten) preexisting in the AssemblyInfo.cs file.
 
The required code snippet is:
[assembly: AssemblyDescription ("Some description about the application, build on 2000-00-00 00:00")]
The code to overwrite the description attribute is:
function pz (i) {
	return (i < 10) ? "0"+i : ""+i;
}
// change the timestamp in the AssemblyDescription attribute
r = /\[assembly:\s*(System\.Reflection\.)?AssemblyDescription(?:Attribute)?\s*\(\s*"([^\d]*)(\d{4}-\d{1,2}-\d{1,2})( \d{1,2}:\d{1,2}(?::\d{1,2})?)?([^\d\"]*)"\s*\)\s*\]/g;
a = r.exec(t);
if (a != null && a.length > 4) {
	var d = new Date();
	v = "[assembly: "+a[1]+"AssemblyDescription (\""+a[2]+d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+pz(d.getHours())+":"+pz(d.getMinutes())+a[5]+"\")]";
	t = t.replace(r, v);
	WScript.StdOut.WriteLine ("Assembly description changed: " + a[0] + "->" + v);
}
 
Note: The (\d{4}-\d{1,2}-\d{1,2}) part of the regular expression matches the "yyyy-MM-dd" part in the attribute content. The ( \d{1,2}:\d{1,2}(?::\d{1,2})?)? part matches the " HH:mm:ss" part (":ss" is optional). You may want to rewrite them to match your needs. Of course, you have to modify the "getFullYear()...." blah blah part to match the pattern of the regular expression, otherwise the rewritten attribute won't get matched the next time.
 
The above code should be placed before the
s.Position = 0;
line in the tip in order to work.
If you are to use this snippet, you might want to use it in the pre-build event rather than the post-build event.

License

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

Share

About the Author

wmjordan

China China
Chinese Poetry Lover.
Programmer.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 3 Oct 2011
Article Copyright 2011 by wmjordan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid