Search code examples
c#.netvisual-studiodeploymentvisual-studio-2005

Determine assembly version during a post-build event


Let's say I wanted to create a static text file which ships with each release. I want the file to be updated with the version number of the release (as specified in AssemblyInfo.cs), but I don't want to have to do this manually.

I was hoping I could use a post-build event and feed the version number to a batch file like this:

call foo.bat $(AssemblyVersion)

However I can't find any suitable variable or macro to use.

Is there a way to achieve this that I've missed?


Solution

  • If you prefer scripting these methods might also work for you:

    If you are using the post-build event, you can use the filever.exe tool to grab it out of the already built assembly:

    for /F "tokens=4" %%F in ('filever.exe /B /A /D bin\debug\myapp.exe') do (
      set VERSION=%%F
    )
    echo The version is %VERSION%
    

    Get filever.exe from here: http://support.microsoft.com/kb/913111

    If you are using the pre-build event, you can take it out of the AssemblyInfo.cs file as follows:

    set ASMINFO=Properties\AssemblyInfo.cs
    FINDSTR /C:"[assembly: AssemblyVersion(" %ASMINFO% | sed.exe "s/\[assembly: AssemblyVersion(\"/SET CURRENT_VERSION=/g;s/\")\]//g;s/\.\*//g" >SetCurrVer.cmd
    CALL SetCurrVer.cmd
    DEL SetCurrVer.cmd
    echo Current version is %CURRENT_VERSION%
    

    This uses the unix command line tool sed, which you can download from many places, such as here: http://unxutils.sourceforge.net/ - iirc that one works ok.