Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Display Version Information

From Wiki

Jump to: navigation, search

Summary: An example of how we can use an AssemblyInfo file to show version information on our page

There may be cases when we would like to display some information about our application (such as it's name, description or build number). Fortunately, we can enter this information ourselves by creating a file named AssemblyInfo.vb and placing it in the App_Code folder. Here's an example of what to include in this file:

  1. Imports System  
  2. Imports System.Reflection  
  3. Imports System.Runtime.InteropServices  
  4.  
  5. ' General Information about an assembly is controlled through the following  
  6. ' set of attributes. Change these attribute values to modify the information  
  7. ' associated with an assembly.  
  8.  
  9. ' Review the values of the assembly attributes  
  10.  
  11. <Assembly: AssemblyTitle("Title goes here")>  
  12. <Assembly: AssemblyDescription("Description goes here")>  
  13. <Assembly: AssemblyCompany("Company goes here")>  
  14. <Assembly: AssemblyProduct("Product goes here")>  
  15. <Assembly: AssemblyCopyright("Copyright goes here")>  
  16. <Assembly: AssemblyTrademark("Trademark goes here")>  
  17. <Assembly: CLSCompliant(True)>  
  18.  
  19. 'The following GUID is for the ID of the typelib if this project is exposed to COM  
  20. <Assembly: Guid("BFC0B506-B50B-457F-BC84-A104B8B22035")>  
  21.  
  22. ' Version information for an assembly consists of the following four values:  
  23. '  
  24. '      Major Version  
  25. '      Minor Version  
  26. '      Build Number  
  27. '      Revision  
  28. '  
  29. ' You can specify all the values or you can default the Build and Revision Numbers  
  30. ' by using the '*' as shown below:  
  31.  
  32. <Assembly: AssemblyVersion("1.0.*")>

You can see that we have sections where we can enter attributes such as the Title and Description of the site. Also, at the bottom of the file, we can set the version information of the site. You can do this by including the full major, minor, build and revision numbers yourself e.g. (1.0.1.1) or you can just set the major and minor numbers and use a wildcard for the build and revision so that ASP.NET generates them automatically (which is what I have done in the example above).

To display this information, I've created a simple page with a label to display the data:

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VersionInformation.aspx.vb" Inherits="VersionInformation" %>  
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>Untitled Page</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </html>

We then need to use the System.Reflection class to iterate through all of this information and display it on the page:

  1. Imports System.Reflection  
  2. Imports System.Text  
  3. Partial Class VersionInformation
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  7.         'Declarations  
  8.         Dim assembly As System.Reflection.Assembly
  9.         Dim sb As New StringBuilder
  10.  
  11.         ' Load the assembly info  
  12.         assembly = System.Reflection.Assembly.Load("App_Code")
  13.  
  14.         ' Iterate through all the attributes for the assembly.  
  15.         For Each attr As Attribute In Attribute.GetCustomAttributes(assembly)
  16.  
  17.             ' Append each value to the StringBuilder  
  18.             Select Case attr.GetType.ToString
  19.  
  20.                 Case "System.Reflection.AssemblyTitleAttribute"
  21.                     sb.Append("Title: " & CType(attr, AssemblyTitleAttribute).Title & "<br>")
  22.  
  23.                 Case "System.Reflection.AssemblyDescriptionAttribute"
  24.                     sb.Append("Description: " & CType(attr, AssemblyDescriptionAttribute).Description & "<br>")
  25.  
  26.                 Case "System.Reflection.AssemblyCompanyAttribute"
  27.                     sb.Append("Company: " & CType(attr, AssemblyCompanyAttribute).Company & "<br>")
  28.  
  29.                 Case "System.Reflection.AssemblyProductAttribute"
  30.                     sb.Append("Product: " & CType(attr, AssemblyProductAttribute).Product & "<br>")
  31.  
  32.                 Case "System.Reflection.AssemblyCopyrightAttribute"
  33.                     sb.Append("Copyright: " & CType(attr, AssemblyCopyrightAttribute).Copyright & "<br>")
  34.  
  35.                 Case "System.Reflection.AssemblyTrademarkAttribute"
  36.                     sb.Append("Trademark: " & CType(attr, AssemblyTrademarkAttribute).Trademark & "<br>")
  37.  
  38.                 Case "System.CLSCompliantAttribute"
  39.                     sb.Append("CLS Compliant: " & CType(attr, CLSCompliantAttribute).IsCompliant & "<br>")
  40.  
  41.                 Case "System.Runtime.InteropServices.GuidAttribute"
  42.                     sb.Append("Guid: " & CType(attr, System.Runtime.InteropServices.GuidAttribute).Value & "<br>")
  43.  
  44.             End Select
  45.  
  46.         Next
  47.  
  48.         ' Add the version info  
  49.         sb.Append("Website version: " & assembly.GetName().Version.ToString())
  50.  
  51.         ' Write out the details  
  52.         Label1.Text = sb.ToString
  53.     End Sub
  54. End Class

When we run this page, we will see an output similar to this:

  1. Company: Company goes here  
  2. Title: Title goes here  
  3. Description: Description goes here  
  4. CLS Compliant: True  
  5. Product: Product goes here  
  6. Guid: BFC0B506-B50B-457F-BC84-A104B8B22035  
  7. Trademark: Trademark goes here  
  8. Copyright: Copyright goes here  
  9. Website version: 1.0.2662.16081


This Hack is part of the ASP.NET Hacks collection

430 Rating: 3.2/5 (10 votes cast)