Write C# Code in your NAnt Build File

When rockin' out the NAnt build process, I thought it was necessary to have a folder that accurately states the build version/number of the enclosed assembly. I'm sure I could have done this w/code, but eff that... I wanted to have a bit of fun, trying the <code> tags from within the build file itself.

Given a path to an assembly, ${build.dir} will echo something like:


I've found it handy for:

  • Doing something at build time wo/another console app.
  • Keeping track of builds (duh)
  • Impressing the ladies.

(Sorry about the whacked formatting!)

<property name="build.dir" value="build" />
<property name="pathToAssembly" value="C:\your\moms\house\nice.dll" />
<target name="getbuildnum">
<script language="C#" prefix="method" >
<import namespace="System.Reflection" />
public string GetVersionFromAssembly(string assPath) {
Assembly ass = Assembly.LoadFrom(assPath);
return "build-"+ass.GetName().Version.ToString()
<property name='build.dir' value='${method::get-version-from-assembly(pathToAssembly)}' />
<echo message='${build.dir}' />

kick it on DotNetKicks.com


About Brian

profile for bluevoodoo1 on Stack Exchange, a network of free, community-driven Q&A sites

Brian Canzanella brings you nifty tips and tricks for most things .NET. read more...

Readers / Stuff