To let people know what version of the program they are using, i want to show the productversion in the title of the window. I can do that manually, but i want this to be dynamic, so i don't have to change both items everytime the version changes.
Is this possible doing this in code behind without messing with the installer?
The version number is stored in the assembly manifest along with other identity information, including the assembly name and public key, as well as information on relationships and identities of other assemblies connected with the application.
How do I programmatically change the Title in a wpf window? Change the Title from "Contacts" to "Something new" when the program finds new information as it starts. You should use binding. Bind the title property of window to a property of your DataContext.
You can retrieve 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 version of the setup from the MSI information stored in the Registry. This is best done based on the UpgradeCode specified by your setup as the UpgradeCode should not change between versions. The following sample program demonstrates how to get the installed version(s)1 belonging 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;
}
}
1Note that there might be several versions of one product be installed in parallel. In that rare case you would get a list with all installed versions.
try this:System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With