About Dialog — How to display assembly attributes in an About Box

Problem

The Problem!

Solution

The Solution!

Discussion

The Discussion!

Example

Code to show the dialog box.

Visual Basic
 
    Private Sub menuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuHelpAbout.Click
        Dim dialog As New AboutDialog
        dialog.ShowDialog(Me)
        dialog.Dispose()
    End Sub
		

Source

Sample about dialog box that lists information about loaded assemblies.

Visual Basic
 
'
'   Copyright © 2004, Jay B. Harlow, All Rights Reserved.
'
Option Strict On
Option Explicit On 

Imports System.Reflection

Public Class AboutDialog
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Private WithEvents labelProduct As System.Windows.Forms.Label
    Private WithEvents labelProductVersion As System.Windows.Forms.Label
    Private WithEvents textProduct As System.Windows.Forms.TextBox
    Private WithEvents textProductVersion As System.Windows.Forms.TextBox
    Private WithEvents labelCompany As System.Windows.Forms.Label
    Private WithEvents textCompany As System.Windows.Forms.TextBox
    Private WithEvents columnAssembly As System.Windows.Forms.ColumnHeader
    Private WithEvents columnTitle As System.Windows.Forms.ColumnHeader
    Private WithEvents columnProduct As System.Windows.Forms.ColumnHeader
    Private WithEvents columnConfiguration As System.Windows.Forms.ColumnHeader
    Private WithEvents columnProductVersion As System.Windows.Forms.ColumnHeader
    Private WithEvents columnAssemblyVersion As System.Windows.Forms.ColumnHeader
    Private WithEvents listAssemblies As System.Windows.Forms.ListView
    Private WithEvents buttonOk As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.labelProduct = New System.Windows.Forms.Label
        Me.labelProductVersion = New System.Windows.Forms.Label
        Me.textProduct = New System.Windows.Forms.TextBox
        Me.textProductVersion = New System.Windows.Forms.TextBox
        Me.labelCompany = New System.Windows.Forms.Label
        Me.textCompany = New System.Windows.Forms.TextBox
        Me.listAssemblies = New System.Windows.Forms.ListView
        Me.columnAssembly = New System.Windows.Forms.ColumnHeader
        Me.columnAssemblyVersion = New System.Windows.Forms.ColumnHeader
        Me.columnTitle = New System.Windows.Forms.ColumnHeader
        Me.columnProduct = New System.Windows.Forms.ColumnHeader
        Me.columnProductVersion = New System.Windows.Forms.ColumnHeader
        Me.columnConfiguration = New System.Windows.Forms.ColumnHeader
        Me.buttonOk = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'labelProduct
        '
        Me.labelProduct.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.labelProduct.Location = New System.Drawing.Point(16, 16)
        Me.labelProduct.Name = "labelProduct"
        Me.labelProduct.TabIndex = 0
        Me.labelProduct.Text = "Product"
        Me.labelProduct.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'labelProductVersion
        '
        Me.labelProductVersion.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.labelProductVersion.Location = New System.Drawing.Point(16, 48)
        Me.labelProductVersion.Name = "labelProductVersion"
        Me.labelProductVersion.TabIndex = 2
        Me.labelProductVersion.Text = "Product Version"
        Me.labelProductVersion.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'textProduct
        '
        Me.textProduct.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.textProduct.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textProduct.Location = New System.Drawing.Point(128, 16)
        Me.textProduct.Name = "textProduct"
        Me.textProduct.ReadOnly = True
        Me.textProduct.Size = New System.Drawing.Size(304, 15)
        Me.textProduct.TabIndex = 1
        Me.textProduct.TabStop = False
        Me.textProduct.Text = ""
        '
        'textProductVersion
        '
        Me.textProductVersion.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.textProductVersion.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textProductVersion.Location = New System.Drawing.Point(128, 48)
        Me.textProductVersion.Name = "textProductVersion"
        Me.textProductVersion.ReadOnly = True
        Me.textProductVersion.Size = New System.Drawing.Size(304, 15)
        Me.textProductVersion.TabIndex = 3
        Me.textProductVersion.TabStop = False
        Me.textProductVersion.Text = ""
        '
        'labelCompany
        '
        Me.labelCompany.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.labelCompany.Location = New System.Drawing.Point(16, 80)
        Me.labelCompany.Name = "labelCompany"
        Me.labelCompany.TabIndex = 4
        Me.labelCompany.Text = "Company"
        Me.labelCompany.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'textCompany
        '
        Me.textCompany.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.textCompany.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.textCompany.Location = New System.Drawing.Point(128, 80)
        Me.textCompany.Name = "textCompany"
        Me.textCompany.ReadOnly = True
        Me.textCompany.Size = New System.Drawing.Size(304, 15)
        Me.textCompany.TabIndex = 5
        Me.textCompany.TabStop = False
        Me.textCompany.Text = ""
        '
        'listAssemblies
        '
        Me.listAssemblies.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.listAssemblies.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.columnAssembly, Me.columnAssemblyVersion, Me.columnTitle, Me.columnProduct, Me.columnProductVersion, Me.columnConfiguration})
        Me.listAssemblies.Location = New System.Drawing.Point(16, 112)
        Me.listAssemblies.Name = "listAssemblies"
        Me.listAssemblies.Size = New System.Drawing.Size(416, 136)
        Me.listAssemblies.TabIndex = 6
        Me.listAssemblies.View = System.Windows.Forms.View.Details
        '
        'columnAssembly
        '
        Me.columnAssembly.Text = "Assembly"
        Me.columnAssembly.Width = 150
        '
        'columnAssemblyVersion
        '
        Me.columnAssemblyVersion.Text = "Assembly Version"
        Me.columnAssemblyVersion.Width = 90
        '
        'columnTitle
        '
        Me.columnTitle.Text = "Title"
        Me.columnTitle.Width = 150
        '
        'columnProduct
        '
        Me.columnProduct.Text = "Product"
        Me.columnProduct.Width = 90
        '
        'columnProductVersion
        '
        Me.columnProductVersion.Text = "Product Version"
        Me.columnProductVersion.Width = 90
        '
        'columnConfiguration
        '
        Me.columnConfiguration.Text = "Configuration"
        Me.columnConfiguration.Width = 120
        '
        'buttonOk
        '
        Me.buttonOk.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.buttonOk.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.buttonOk.Location = New System.Drawing.Point(360, 256)
        Me.buttonOk.Name = "buttonOk"
        Me.buttonOk.TabIndex = 7
        Me.buttonOk.Text = "Ok"
        '
        'AboutDialog
        '
        Me.AcceptButton = Me.buttonOk
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.CancelButton = Me.buttonOk
        Me.ClientSize = New System.Drawing.Size(442, 288)
        Me.Controls.Add(Me.buttonOk)
        Me.Controls.Add(Me.listAssemblies)
        Me.Controls.Add(Me.textCompany)
        Me.Controls.Add(Me.textProductVersion)
        Me.Controls.Add(Me.textProduct)
        Me.Controls.Add(Me.labelCompany)
        Me.Controls.Add(Me.labelProductVersion)
        Me.Controls.Add(Me.labelProduct)
        Me.Name = "AboutDialog"
        Me.Text = "About Sample Program"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Me.textProduct.Text = Application.ProductName
        Me.textProductVersion.Text = Application.ProductVersion
        Me.textCompany.Text = Application.CompanyName
        For Each [assembly] As [assembly] In AppDomain.CurrentDomain.GetAssemblies()
            Dim name As AssemblyName = [assembly].GetName()
            Dim item As New ListViewItem

            item.Text = GetAssemblyName([assembly])
            With item.SubItems
                .Add(GetAssemblyVersion([assembly]))
                .Add(GetAssemblyTitle([assembly]))
                .Add(GetProductName([assembly]))
                .Add(GetProductVersion([assembly]))
                .Add(GetConfiguration([assembly]))
                .Add(GetFileVersion([assembly]))
            End With

            Me.listAssemblies.Items.Add(item)
        Next
        Me.listAssemblies.ListViewItemSorter = New ListViewItemComparer(0, True)

    End Sub

    Private Function GetAssemblyName(ByVal [assembly] As [Assembly]) As String
        Return [assembly].GetName().Name
    End Function

    Private Function GetAssemblyVersion(ByVal [assembly] As [Assembly]) As String
        Return [assembly].GetName().Version.ToString()
    End Function

    Private Function GetAssemblyTitle(ByVal [assembly] As [Assembly]) As String
        Dim attributes() As Object
        attributes = [assembly].GetCustomAttributes(GetType(AssemblyTitleAttribute), False)
        If attributes.Length > 0 Then
            Dim assemblyTitle As AssemblyTitleAttribute = DirectCast(attributes(0), AssemblyTitleAttribute)
            Return assemblyTitle.Title
        Else
            Return String.Empty
        End If
    End Function

    Private Function GetProductName(ByVal [assembly] As [Assembly]) As String
        Dim attributes() As Object
        attributes = [assembly].GetCustomAttributes(GetType(AssemblyProductAttribute), False)
        If attributes.Length > 0 Then
            Dim assemblyProduct As AssemblyProductAttribute = DirectCast(attributes(0), AssemblyProductAttribute)
            Return assemblyProduct.Product
        Else
            Return String.Empty
        End If
    End Function

    Private Function GetProductVersion(ByVal [assembly] As [Assembly]) As String
        Dim attributes() As Object
        attributes = [assembly].GetCustomAttributes(GetType(AssemblyInformationalVersionAttribute), False)
        If attributes.Length > 0 Then
            Dim assemblyProductVersion As AssemblyInformationalVersionAttribute = DirectCast(attributes(0), AssemblyInformationalVersionAttribute)
            Return assemblyProductVersion.InformationalVersion
        Else
            Return String.Empty
        End If
    End Function

    Private Function GetConfiguration(ByVal [assembly] As [Assembly]) As String
        Dim attributes() As Object
        attributes = [assembly].GetCustomAttributes(GetType(AssemblyConfigurationAttribute), False)
        If attributes.Length > 0 Then
            Dim assemblyConfiguration As AssemblyConfigurationAttribute = DirectCast(attributes(0), AssemblyConfigurationAttribute)
            Return assemblyConfiguration.Configuration
        Else
            Return String.Empty
        End If
    End Function

    Private Function GetFileVersion(ByVal [assembly] As [Assembly]) As String
        Dim attributes() As Object
        attributes = [assembly].GetCustomAttributes(GetType(AssemblyFileVersionAttribute), False)
        If attributes.Length > 0 Then
            Dim assemblyFileVersion As AssemblyFileVersionAttribute = DirectCast(attributes(0), AssemblyFileVersionAttribute)
            Return assemblyFileVersion.Version
        Else
            Return String.Empty
        End If
    End Function

    Private Sub listAssemblies_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles listAssemblies.ColumnClick
        Dim comparer As ListViewItemComparer = DirectCast(listAssemblies.ListViewItemSorter, ListViewItemComparer)
        If comparer.Column = e.Column Then
            Me.listAssemblies.ListViewItemSorter = New ListViewItemComparer(e.Column, Not comparer.Ascending)
        Else
            Me.listAssemblies.ListViewItemSorter = New ListViewItemComparer(e.Column, True)
        End If
    End Sub

    Private Class ListViewItemComparer
        Implements IComparer

        Private ReadOnly m_column As Integer
        Private ReadOnly m_ascending As Boolean

        Public Sub New(ByVal column As Integer, ByVal ascending As Boolean)
            m_column = column
            m_ascending = ascending
        End Sub

        Public ReadOnly Property Column() As Integer
            Get
                Return m_column
            End Get
        End Property

        Public ReadOnly Property Ascending() As Boolean
            Get
                Return m_ascending
            End Get
        End Property

        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
            Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
            Dim value1 As String
            Dim value2 As String
            If m_ascending Then
                value1 = item1.SubItems(m_column).Text
                value2 = item2.SubItems(m_column).Text
            Else
                value1 = item2.SubItems(m_column).Text
                value2 = item1.SubItems(m_column).Text
            End If
            Return String.Compare(value1, value2)
        End Function

    End Class

End Class