How to display the running version in a C# WPF app

If you’re writing C# Windows Presentation Foundation (WPF) apps it’s useful to display the current version of the app on the main window (or at least accessible through a menu or button). You could hardcode the version and change it each time you update the code, but that adds an unnecessary maintenance overhead which you can forget to do.

If you publish your application in Visual Studio as a ClickOnce application, the version automatically increments each time you publish. This article shows how to display that version on the main window of your application.

1. Give your main window a name

Not necessary to get the version, but useful if you want to display the version on the main window.

Double-click on the MainWindow.xaml file in the Visual Studio Solution Explorer to get to the designer, and click on the main window. Then give it a name in the Properties tab, or directly edit the XAML:

<Window x:Name="mainWindow" x:Class="myapp.MainWindow">

2. Include the libraries you need, and add a reference

The methods you need are found in the ApplicationDeployment and Reflection namespaces. Add the following using statements to MainWindow.xaml.cs:

using System.Deployment.Application;
using System.Reflection;

You will also need to add a reference to the System.Deployment assembly by right-clicking on References in the Solution Explorer and selecting Add Reference.

3. Define a method to get the version

Depending on how you set the version (or how the version is automatically set), it could be set in the ApplicationDeployment.CurrentDeployment properties, or in the executing assembly at runtime. This method checks both. Add it in the MainWindow : Window partial class:

private Version getRunningVersion()
      return ApplicationDeployment.CurrentDeployment.CurrentVersion;
   catch (Exception)
      return Assembly.GetExecutingAssembly().GetName().Version;

4. Display the version

To display the version to the main window title, call the method in the MainWindow constructor after the call to InitializeComponent():

public MainWindow()

    // display version
    mainWindow.Title = "myapp " + getRunningVersion();

See this discussion in Stack Overflow on how to get the version and why you need the try-catch block:

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s