rss search

Getting Version Number in Asp.NET

line

One of my recent tasks was to get Version Number information from AssemblyInfo.cs in Web Application project. So here I will try to explain what I did in order to accomplish my task:

There are several ways of doing this but I can’t say all of them are reliable methods. First methods I found are like the followings:

public static string Version {
    get {
        Assembly asm = Assembly.GetExecutingAssembly();
        FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
        returnString.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
    }
}

But the problem is what if your current method or your type is defined within another assembly and the method above will bring that particular assembly AssemblyInfo.cs information. But we specifically want our Web Application’s version number. If you are confused, think of it like this: You are working with types such as string, int, DateTime, Random class etc. They are all defined in an Assembly. So String is defined in System namespace which resides in mscore.dll and this DLL file has an assembly info data that describes some information about it such as VersionNumber, ProductVersion, etc. As long as all these assemblies are locating in searchable directories such as GAC, bin, etc. whenever a type is called, these assemblies will be searched and will be loaded to the Assembly Cache. So during programming life time, it may be hard to say in which assembly is the “ExecutingAssembly()”since you may define your method in different Assembly but want AssemblyInfo.cs of Web Project (consider there other class library project in your solution).

Anyways, long story short I’ve found the following code more reliable:

public static string Version {
    get {
        Assembly asm = typeof(ACustomClassDefinedInYourWebApp).Assembly
        return asm.GetName().Version;
    }
}

A CustomClassDefinedInYourWebApp can be the class where you define the method above within. So you will always be sure that you have the correct Assembly to get its AssemblyInfo.cs.

Happy coding!


8 comments

line
  1. Yalin

    I ran into a similar requirement (Created the requirement myself) and chose a different route to managing revision numbers, since I go by TFS Revision number, and hard coded version number in Web.config.

    So, here’s how it works…

    Step 1 – Pre-Build event for the project writes the latest TFS Version of the project into a file using tf.exe

    IF “$(DevEnvDir)” == “*Undefined*” GOTO end
    “$(DevEnvDir)tf” checkout /lock:none “$(ProjectDir)output.txt”
    call “$(DevEnvDir)tf” history “$(ProjectDir)” /r /stopafter:1 /i /version:W > “$(ProjectDir)version.txt”
    :end
    exit 0

    Step 2 – Static property reads the file

    public static int GetRevisionNumber
    {
    get
    {
    int buildVersion = 0;
    string fileLocation = AppDomain.CurrentDomain.BaseDirectory + “version.txt”;
    try
    {
    using (TextReader tw = new StreamReader(fileLocation))
    {
    tw.ReadLine();
    tw.ReadLine();
    string data = tw.ReadLine();
    data = data.Substring(0, data.IndexOf(” “));
    int.TryParse(data, out buildVersion);
    buildVersion++;
    }
    }
    catch { }
    //System.IO.File.Delete(fileLocation);
    return buildVersion;
    }
    }

    ps. it would be nice if there were formatting options in the comments. ;)

    line

Leave a Reply