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
<code>
using System.Reflection;
</code>

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

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.

<code>
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.

<code>
Version version = app.GetName().Version;
</code>

Now you can use the metadata in your app.
<code>
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;
</code>
That’s how you make an about box – Hoboken style!

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

Tom

Leave a comment

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