Your Application Title in .NET

Posted Mon, Aug 27 2012 15:36 by Deborah Kurata

You would think that giving an application a title and later retrieving that title would be a straightforward thing to do in VB.NET or C#. But it turns out to be a little complex, especially to find the appropriate place to enter the title then to write the appropriate code to retrieve that title.

First, enter the title of the application.

In C#:

  1. Find the project in Solution Explorer.
  2. Double-click on the Properties node under the project in Solution Explorer.
  3. Select the Application tab.
  4. Click the Assembly Information button.
  5. Enter the desired title into the provided dialog.

image

In VB:

  1. Find the project in Solution Explorer.
  2. Double-click on the My Project node under the project in Solution Explorer.
  3. Select the Application tab.
  4. Click the Assembly Information button.
  5. Enter the desired title into the provided dialog.

image

Second, write the code to retrieve that title. In this example, the title is provided as an ApplicationName property.

In C#:

/// <summary>
/// Gets the name of the application.
/// </summary>
/// <value></value>
/// <remarks></remarks>
public static string ApplicationName
{
    get
    {
        var entryAssembly = Assembly.GetEntryAssembly();
        var applicationTitle = ((AssemblyTitleAttribute)entryAssembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title;
        if (string.IsNullOrWhiteSpace(applicationTitle))
        {
            applicationTitle = entryAssembly.GetName().Name;
        }
        return applicationTitle;
    }
}

In VB:

''' <summary>
''' Gets the name of the application.
''' </summary>
''' <value></value>
''' <remarks></remarks>
Public Shared ReadOnly Property ApplicationName() As String
    Get
        Dim applicationTitle = My.Application.Info.Title
        If String.IsNullOrWhiteSpace(My.Application.Info.Title) Then
            applicationTitle = My.Application.Info.AssemblyName
        End If
        Return applicationTitle
    End Get
End Property

The C# code defines a static property for the application name. Its getter first determines the entry assembly, which is the first executable that was executed. So even if this property is in a library component, if the application was launched with a WinForms or other UI component, the entry assembly is the UI component and not the library. If the code needs the currently executing assembly, replace GetEntryAssembly with GetExecutingAssembly.

The C# code then uses GetCustomAttributes to obtain the AssemblyTitleAttribute from the assembly to then retrieve the title.

If for some reason the title attribute was not set, the code uses the GetName method to retrieve the name of the entry assembly and uses the retrieved name.

The VB code uses the My namespace to retrieve the application title. The My namespace provides a very nice shortcut to the application's title attribute. If for some reason the title attribute was not set, the code uses the AssemblyName, also provided within the My namespace.

In both languages, the application title is "Acme Customer Management" as defined in the Assembly Information dialog.

Use this technique any time you want to define the application title in the assembly information and retrieve it for display or logging.

Enjoy!

Filed under: , , ,

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: