Can I indicate the version of the product in the window title?

To let people know which version of the program they are using, I want to show productversion in the window title. I can do it manually, but I want it to be dynamic, so I don't need to change both elements every time the version changes.

Is it possible to do this in the code behind without starting with the installer?

+6
c # visual-studio version
source share
4 answers

You can get the version from the AssemblyName.Version property using the following code:

 Version version = Assembly.GetExecutingAssembly().GetName().Version; this.Text = "My Cool Product - Version " + version; // or with a fancier formatting this.Text = string.Format("My Cool Product - Version {0}.{1}.{2} Revision {3}", version.Major, version.Minor, version.Build, version.Revision); 

Update (after comment):

You can also read the installation version from the MSI information stored in the registry. This is best done based on the UpgradeCode code specified by your setup, since UpgradeCode should not change between versions. The following sample program demonstrates how to get installed version 1 related to a specific UpgradeCode:

 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; class Program { [DllImport("msi.dll", CharSet = CharSet.Unicode)] static extern Int32 MsiGetProductInfo(string product, string property, [Out] StringBuilder valueBuf, ref Int32 len); [DllImport("msi.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern Int32 MsiEnumRelatedProducts(string strUpgradeCode, int reserved, int iIndex, StringBuilder sbProductCode); static void Main(string[] args) { List<string> installedVersions = GetInstalledVersions("{169C1A82-2A82-490B-8A9A-7AB7E4C7DEFE}"); foreach (var item in installedVersions) { Console.WriteLine(item); } } static List<string> GetInstalledVersions(string upgradeCode) { List<string> result = new List<string>(); StringBuilder sbProductCode = new StringBuilder(39); int iIdx = 0; while ( 0 == MsiEnumRelatedProducts(upgradeCode, 0, iIdx++, sbProductCode)) { Int32 len = 512; StringBuilder sbVersion = new StringBuilder(len); MsiGetProductInfo(sbProductCode.ToString(), "VersionString", sbVersion, ref len); result.Add(sbVersion.ToString()); } return result; } } 

1 Please note that several versions of one product can be installed in parallel. In this rare case, you will receive a list with all installed versions.

+8
source share

try the following:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

+6
source share

Like this:

 Text = "MyApplication v" + typeof(MyForm).Assembly.GetName().Version; 

This will read the [assembly: AssemblyVersion("...")] attribute from AssemblyInfo.cs, which can also be set in the project properties (by clicking the Assembly Information ... button)

0
source share

Here is my solution using DataBinding and Reflection:

In XAML:

 <Window ... Title="{Binding WindowTitle, RelativeSource={RelativeSource Mode=Self}}" > 

In code:

 public string WindowTitle { get { Version version = Assembly.GetExecutingAssembly().GetName().Version; return "MyTitle v" + version; } } 
0
source share

All Articles