Auto Update of Assembly File Version on Build

748 views Asked by At

I had created a C# Project, that takes a single argument ( being the location of the affected project's AssemblyInfo file ), which then updates the AssemblyFileVersion's Revision number by 1. This program is called from the Properties --> Build Events --> Post-build event command line using:

call "C:\Users\SomeUser\Documents\Visual Studio 2013\Projects\VersionUpdate\VersionUpdate\bin\Release\VersionUpdate.exe" "$(ProjectDir)Properties\AssemblyInfo.cs"

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VersionUpdate
{
class Program
{
    static void Main( string[] args )
    {
        string assemblyInfoFile = args[ 0 ]; //Read In From Command Line Argument 1
        string line; //Holds the line of the Original AssemblyInfo.cs file being read
        string currentAssemblyFileVersion = ""; //Holds the current AssemblyFileVersion
        string newAssemblyFileVersion = ""; //Holds the new AssemblyFileVersion
        string revision = ""; //Holds the revision portion of AssemblyFileVersion ( until end of line )
        string start = ""; //Holds the AssemblyFileVersion line up to and including the last .
        string tail = ""; //Holds the tail of the AssemblyFileVersion after the Revision Number
        int periods = 0; //Holds the number of . that have been counted
        int end = 0; //Holds the index of the final " after the Revision Number
        int revisionNumber = 0; //Holds the integer representation of the Revision Number
        int newRevisionNumber = 0; //Holds the incremented Revision Number

        Regex exp = new Regex( ".*\\bassembly: AssemblyFileVersion\\b.*" ); //Matches the proper line

        //Establish new FileStream for Reading
        FileStream fs = new FileStream( assemblyInfoFile, FileMode.Open, FileAccess.Read, FileShare.None );

        //Create StreamReader to Read AssemblyInfo file
        StreamReader sr = new StreamReader( fs );

        //Loop through AssemblyInfo file searching for a Match
        while ( ( line = sr.ReadLine() ) != null )
        {
            Match test = exp.Match( line );

            //If we found a match, save the matched line as the current AssemblyFileVersion
            if ( test.Success )
            {
                currentAssemblyFileVersion = line;
            }
        }

        //Search the current AssemblyFileVersion to locate the proper position and retrieve the Revision Number
        for ( int i = 0; i < currentAssemblyFileVersion.Length; i++ )
        {
            char piece = currentAssemblyFileVersion[ i ];

            //Look for . and increment periods when found
            if ( piece == '.' )
            {
                periods++;
            }

            //Save the Revision Number once the final . is found
            if ( periods == 3 )
            {
                //Reset periods so that looping does not overwrite results
                periods = 0;

                start = currentAssemblyFileVersion.Substring( 0, i + 1 ); //Store the beginning of the AssemblyFileVersion string, including the final .
                revision = currentAssemblyFileVersion.Substring( i + 1 ); //Store the reaminder of the AssemblyFileVersion string
                end = revision.IndexOf( '\"' ); //Locate the index of the final " after the Revision Number
                tail = revision.Substring( end ); //Store the remaining string after the Revision Number
                revisionNumber = Convert.ToInt16( revision.Substring( 0, end ) ); //Convert the Revision Number to an int
            }
        }

        newRevisionNumber = ++revisionNumber; //Preincrement and store Revision Number
        newAssemblyFileVersion = start + newRevisionNumber + tail; //Create new AssemblyFileVersion string

        //Close the FileStream
        fs.Close();

        //Read the entire AssemblyInfo file and replace the affected line
        string wholeFile = File.ReadAllText( assemblyInfoFile );
        wholeFile = wholeFile.Replace( currentAssemblyFileVersion, newAssemblyFileVersion );
        File.WriteAllText( assemblyInfoFile, wholeFile );
    }
}
}

I then grabbed the AssemblyFileVersion with:

using System.Diagnostics;
using System.Reflection;

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo( assembly.Location );
string version = versionInfo.FileVersion;

I'm posting this to help others in a similar situation, as pertinent information was hard to find, but also, I'm by no means an expert, and was wondering if there was a better way I could have accomplished this.

0

There are 0 answers