Use Assembly Title, Description, Company, Copyright, Version in C#


Overview

When you edit the properties of a project in Visual Studio, you can enter a Title, Description, Company, Copyright and Version in the Assembly Information section. The question becomes, how do you use these in a project so that you have a single source of information? In this article I’ll walk you through the steps used to access the assembly information so you can easily use it throughout your application.

Diving In

First let’s get some background information down for those of you who are interested (if not, skip to Steps below). The information entered in the Assembly Information dialog (Project Properties > Application Tab > Assembly Information Button) is stored in the Properties folder within a file called AssemblyInfo.cs. This file will have entries that look like this.

[assembly: AssemblyTitle("Your Title")]
[assembly: AssemblyDescription("Your Description")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Your Company")]
[assembly: AssemblyProduct("Your Product")]
[assembly: AssemblyCopyright("Your Copyright")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Steps

In order to use this information in our code, we’re going to have to access the assembly. If you haven’t done this before, this might sound kind of scary, but it’s actually pretty straight forward and within a few steps we’ll have you working with the assembly too!

  1. Create a code file in which you wish to have these properties available (AppInfo.cs in my example).
  2. We will be using System.Reflection to get the assembly information, so enter a using statement at the top of the page for this.
    1. using System.Reflection;
  3. Create an empty property with a get accessor for each piece of information you want to access.
    1. public static string Title { get { } }
  4. Within the get section of the property we must access the current running assembly (i.e. your application).
    1. var assm = Assembly.GetExecutingAssembly();
  5. Now we want to get the custom attribute for the assembly title from the current assembly.
    1. var title = (AssemblyTitleAttribute)assm.GetCustomAttribute(typeof(AssemblyTitleAttribute));
  6. Next we simply check the title for contents and return.
    1. if (title != null && !string.IsNullOrEmpty(title.Title))
      return title.Title;
      else
      return string.Empty;

Custom Attributes List

Below are the custom attributes that correspond to the application information.

  • AssemblyTitleAttribute
  • AssemblyDescriptionAttribute
  • AssemblyCompanyAttribute
  • AssemblyCopyrightAttribute
  • AssemblyVersionAttribute

Full Code

I’ve provided a link below (AppInfo) to the full code that contains the custom attribute names for each property. Within the file you’ll see that we simply do the same thing for each of the custom attributes. In the file though, you’ll see that Version is slightly different. This is because it is also returned as a string, but I wanted it returned as a System.Version type. Therefore we parse the output first.

File: AppInfo.doc

Note: This is a .doc file since WordPress doesn’t allow .cs or .txt files. Just copy the contents or rename the file AppInfo.cs after you download it. Add the file to your code, change the namespace to your app name and you’re done.

Summary

That’s it! You now have the assembly information available in your application. Once you build/compile your project, you can use it by simply calling AppInfo.Title, AppInfo.Description and so on. Please let me know if you have any questions and as always, I welcome your comments below!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s