Can i specify the productversion in a window title?
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 th开发者_运维百科is in code behind without messing with the installer?
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();
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;
}
}
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 Project Properties (by clicking the Assembly Information... button)
精彩评论