Plip's Weblog

Phil Winstanley - British .NET chap based in Lancashire. Enjoys tea and tech. Working for Microsoft.

Display the version of all assemblies in an ASP.NET application on a page

More often than not it's quite useful to know if a Web site is running the latest version of assemblies, especially if you have shared common code which is updated regularly.

NameVersion
App_Code.pacsdi-w1.0.0.42746
Ajax5.7.25.1
CustomProviders1.0.0.0

This snippet of code will print out a table of all (relevant - it excludes .NET assemblies or anything with no version) assembly versions to the page. (Yes the code is crude, I just like Olde Worlde Response.Write-esque code!)

    1         this.Controls.Add(new LiteralControl("<table border=1 width=50%><tr><td>Name</td><td>Version</td></tr>"));

    2         Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    3 

    4         foreach (Assembly a in assemblies)

    5         {

    6             //Ignore the Framework Assembly's

    7             string ExcludedNames = ("SystemMicrosoft.JScriptVJSharpCodeProviderCppCodeProviderWebDev.WebHostmscorlib");

    8             if ((a.GetName().Name.ToString().IndexOf("System") < 0) && (a.GetName().Name.ToString().IndexOf("Microsoft") < 0) && (ExcludedNames.IndexOf(a.GetName().Name.ToString()) < 0) && (a.GetName().Version.ToString() != "0.0.0.0"))

    9             {

   10 

   11                 try

   12                 {

   13                     string Version = a.GetName().Version.ToString();

   14                     AssemblyInformationalVersionAttribute[] infoversion = (AssemblyInformationalVersionAttribute[])a.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);

   15                     if (infoversion.Length == 1)

   16                     {

   17                         Version += (" (" + infoversion[0].InformationalVersion.ToString() + ")");

   18                     }

   19                     LiteralControl lit = new LiteralControl();

   20                     lit.Text = "<tr><td>" + a.GetName().Name.ToString() + "</td><td>" + Version + "</td></tr>";

   21 

   22                     this.Controls.Add(lit);

   23                 }

   24                 catch

   25                 {

   26                 }

   27 

   28             }

   29         }

   30         this.Controls.Add(new LiteralControl("</table>"));


If you're using ASP.NET 2.0 this can produce some "odd" looking results because of the new compilation engine. One way to avoid these results is to use the aspnet_merge tool which ships with the Visual Studio 2005 Web Deployment Projects plugin for Visual Studio.
Posted: Feb 05 2006, 12:01 AM by Plip | with 2 comment(s)
Filed under:

Comments

Plip said:

Oh, to see all the ASP.NET 2.0 assemblies take out the Version 0.0.0.0 exclusion.

Cheers,

Phil.
# February 4, 2006 7:03 PM

aschurg said:

Thanks for posting, I was going crazy figuring out how to do this in .NET 1.1

# May 15, 2007 11:09 AM