How to make an about box in C# WPF

I was recently making an about box in WPF – because there isn’t one included. Because of this, I needed to get a hold of the assembly information. I couldn’t find any good documentation on how to do this, so I figured it out myself and I’m posting it here in the hopes that it might help someone out.

First, you need a reference to the system.reflection namespace
using System.Reflection;

Next you need to get a reference to the executing assembly
Assembly app = Assembly.GetExecutingAssembly();

Now you can use the GetCustomAttributes() function from the calling assembly to get the appropriate data.
This is not as cut-and-dry as you would suspect. The GetCustomAttributes() function returns an array of objects. You will also need to index it and cast it to an appropriate type to use the value.

AssemblyTitleAttribute title = (AssemblyTitleAttribute)app.GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0];
AssemblyProductAttribute product = (AssemblyProductAttribute)app.GetCustomAttributes(typeof(AssemblyProductAttribute),false)[0];
AssemblyCopyrightAttribute copyright = (AssemblyCopyrightAttribute)app.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0];
AssemblyCompanyAttribute company = (AssemblyCompanyAttribute)app.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0];
AssemblyDescriptionAttribute description=(AssemblyDescriptionAttribute)app.GetCustomAttributes(typeof(AssemblyDescriptionAttribute),false)[0];</code>

This takes care of everything except the version. To get that, you need to use the getname() function of the executing assembly.

Version version = app.GetName().Version;

Now you can use the metadata in your app.
this.Title = String.Format(“About {0}”, title.Title);
lblTitle.Content = title.Title;
this.lblProductName.Content = product.Product;
this.lblVersion.Content =String.Format(“Version {0}”, version.ToString());
this.lblCopyright.Content = copyright.Copyright.ToString();
this.lblCompanyName.Content = company.Company;
this.Description.Text = description.Description;
That’s how you make an about box – Hoboken style!

(I’m not from New Jersey, I just watch too many shows about food.)


Leave a comment

Your email address will not be published. Required fields are marked *