Your Application Title in .NET
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#:
- Find the project in Solution Explorer.
- Double-click on the Properties node under the project in Solution Explorer.
- Select the Application tab.
- Click the Assembly Information button.
- Enter the desired title into the provided dialog.
In VB:
- Find the project in Solution Explorer.
- Double-click on the My Project node under the project in Solution Explorer.
- Select the Application tab.
- Click the Assembly Information button.
- Enter the desired title into the provided dialog.
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!
sarabjeet — October 3, 2012 @ 9:48 am
Thanks for all the information,it was very helpful and i really like that you are providing information on .net training ,being enrolled in .net freshers training with projects live training http://www.wiziq.com/course/57-fresher-training-projects i was looking for such .net fresher training to assist me and your information helped me a lot.Really like that you are providing such information . Thanks.