Jan 11 2010

Auto Updating Version Text

Category: Joel Ivory Johnson @ 15:11

I was making an application with an About dialog that shows the version number for the app. Rather than add the version number as static text I made use of reflection to extract the version number from the assembly. If you are interested in doing the same in your application you can use the following code to get the version number text.

Regex versionRegex = new Regex("Version=(?\\d\\.\\d\\.\\d\\.\\d)");

String GetVersion()
{
    string fullName = (this.GetType().Assembly.FullName).ToString();
    Match m = versionRegex.Match(fullName);
    if(m.Success)
    {
        return String.Format("{0}", m.Groups["ver"].Value);
    }
    else
    {
        return "";
    }
}

Tags:

Comments

1.
Mike Mike United States says:

Good Stuff - Thanks Joel!

2.
DarkAngel DarkAngel France says:

I personnaly use the "Version.ToString(4);" (or .ToString(3))
something like this this.GetType().Assembly.GetName().Version.ToString(4)

I had a log dll that takes the version of calling assembly using code like this (don't remember wich line)
  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
  System.Reflection.Assembly.GetCallingAssembly().GetName().Version
I prefer those "GetExecutingAssembly" to this.GetType() but this is really a personnal thing :o)

hope this helps!