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#">
        <references>
            <include name="System.dll"/>
            <include name="System.Xml.dll"/>
        </references>
        <imports>
            <import namespace="System"/>
            <import namespace="System.IO"/>
            <import namespace="System.Text"/>
            <import namespace="System.Text.RegularExpressions"/>
        </imports>
        <code><![CDATA[
            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;
                }
            }
        ]]></code>
    </script>
</target>

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.