Getting The Project Version Number Using NAnt

Specifying the file containing the global assembly information:

<property name="project.version.file" value="./AssemblyInfo.cs"/>

Retrieving the version information:

<target name="getVersion" description="Gets the project version number from the assembly information file">
    <script language="C#">
            <include name="System.dll"/>
            <include name="System.Xml.dll"/>
            <import namespace="System"/>
            <import namespace="System.IO"/>
            <import namespace="System.Text"/>
            <import namespace="System.Text.RegularExpressions"/>
            public static void ScriptMain(Project project)
                Regex expression = new Regex("\[assembly: AssemblyVersion\("(.*)"\)\]");

                using (StreamReader reader = File.OpenText(project.Properties["project.version.file"]))
                    string file = reader.ReadToEnd();
                    Match match = expression.Match(file);
                    project.Properties["project.version"] = match.Groups[1].Captures[0].Value;

Usage: ${project.version}

Prior to using the project.version property the getVersion target must be called or the target using the property must be directly or indirectly dependent on the getVersion target.

Note: The previous code applies to C# assembly version information only. A simple alteration of the Regex value will allow the code to work with vb assembly version information.