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.
Dirk vollmar
source share