views:

91

answers:

3

Hey guys,

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?

Thanks in advance.

+6  A: 

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

Botz3000
+5  A: 

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.

0xA3
You should not use `String.Format` here. Also, `typeof(MyForm).Assembly` will be much faster.
SLaks
@SLaks why not use string.Format to format a string?
Pete Kirkham
@SLaks: It's true that this is slightly slower, but I doubt this will ever be significant (unless you want to query the version 1000 times per second, but then you have another much bigger problem ;-). It should however be noted that `Assembly.GetExecutingAssembly()` and `typeof(MyForm).Assembly` can be different things so you should carefully choose whatever you need.
0xA3
@Pete: Why force a string parse when you don't have to?
SLaks
your version is giving 1.0.0.0, while the setup is set to 1.0.16, why is this?
djerry
@djerry: Setup version and assembly version are different things. The assembly version is specified in the file AssemblyInfo.cs in the *Properties* subfolder of your project.
0xA3
instead of asking for assemblyversion, it's much easier to just manually put it in the title then?
djerry
@djerry: See my update for how to get the version specified by the setup.
0xA3
You really should update your assembly version to match the installation package version
Scott P
@Scott P: A ProductVersion must not necessarily be equal to an AssemblyVersion. What if your product consists of several assemblies? Each assembly might have individual versioning applied then. Or what, if you just update external resources and ship a new version? Then your assembly version would remain unchanged.
0xA3
@Scott P: And I forgot to mention that there are also reasons to leave the assembly version unchanged, even if they contain a newer version of your code as a new assembly version might be a breaking change, see http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57148.aspx
0xA3
I don't agree with Suzanne's logic. I update all the file version / assembly versions in a solution together. The build number changes with every change. I change the minor revision with each production build. I think of the file version being superfluous.
Scott P
I can see how the production version can diverge from the installer version, especially in the case of patching existing installations. However, I'm not sure I would want to display the installer version number from my executable instead of the product version number.
Scott P
Well, all together, i'm not getting any info of the updated method
djerry
@djerry: Did you use the upgrade code specified in your setup and is the product actually installed? Otherwise you won't get anything back. The upgrade code used in my code is just a fictitious sample.
0xA3
yes i have put the code from my setup in it, and tried both while debugging (figured it was obvious that wouldn't work) but after install still no feedback.
djerry
@djerry: as far as I remember, the UpgradeCode GUID needs to be in curly braces and all letters need to be uppercase. Can you please check that this is the case? Also make sure not to mix up UpgradeCode and ProductCode.
0xA3
{0B857052-6458-4CD4-B04C-0825BB29E148} i compared it to your code, so i know it was the right one
djerry
@djerry: If you are on a 64-bit OS also make sure that you don't use the AnyCPU setting as this might affect correct retrieval of the MSI information.
0xA3
i am not, i'm using the x86 version for other reasons too
djerry
A: 

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)

SLaks