简体   繁体   中英

How do I programmatically get the product (not assembly) version of a running program using C#?

I have a program that I have written and am trying to create an about box for. I recently updated my program's product version to 1.00.0003, and I want this to be reflected in the about window.

The default setup of the aboutBox shows a value of 1.0.0.0, which is the assembly version, not the product version. I have since been scouring the Internet to find how to get the product version to be shown. I have tried all of these:

{
    Assembly assembly = Assembly.GetExecutingAssembly();
    FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
    string version = fileVersionInfo.ProductVersion;

    Debug.WriteLine(version);
    Debug.WriteLine(assembly.GetName().Version);
    string v = VersionNumber;
    Debug.WriteLine(v);
    Debug.WriteLine( fileVersionInfo.FileVersion);
    Debug.WriteLine(Application.ProductVersion);
    Debug.WriteLine(AssemblyProductVersion);


    Assembly assembly2 = Assembly.GetEntryAssembly();
    FileVersionInfo fileVersionInfo2 = FileVersionInfo.GetVersionInfo(assembly.Location);
    string version2 = fileVersionInfo2.ProductVersion;
    Debug.WriteLine(version2);
    Debug.WriteLine(assembly2.GetName().Version);

    return version;
}

private string _ourVersion = "Version: v";

private string VersionNumber
{
    get
    {
        System.Reflection.Assembly _assemblyInfo =
        System.Reflection.Assembly.GetExecutingAssembly();
        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            _ourVersion += ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
        else
        {
            if (_assemblyInfo != null)
                _ourVersion += _assemblyInfo.GetName().Version.ToString();
        }
        return _ourVersion;
    }
}

private static string AssemblyProductVersion
{
    get
    {
        object[] attributes = Assembly.GetExecutingAssembly()
            .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
        return attributes.Length == 0 ?
            "" :
            ((AssemblyInformationalVersionAttribute)attributes[0]).InformationalVersion;
    }
}

Every single one of these returns 1.0.0.0 (yes, I did look for their output in the console, not what was actually displayed), instead 1.00.0003 like I need. The product version is set in the General Information tab of the InstallShield setup. When it is installed, going to Programs and Features shows a Product Version of 1.00.0003, so I cannot figure out why this is so hard to programmatically retrieve this value. Any ideas?

您的产品版本应与程序集版本匹配 - 请查看如何使产品版本属性自动匹配可执行文件的版本号

The version 1.00.0003 you want to retrieve is the version of the installer of your product. To get this version programmatically you need to inspect the installer (MSI file), not the installed files. I'm not sure that is what you want to do but there is a answer to the question Checking ProductVersion of an MSI programatically that explains how to do that.

If you want your executable files to contain the same version number you need to store the version number in some way either using a .NET attribute like AssemblyFileVersion or a Windows VERSIONINFO resource.

InstallShield allows you to specify the product version on the command line. This allows you to store your product version in a single file and then use that as the source of both the product version embedded in your installer as well as AssemblyFileVersion of your assemblies.

If only the installer knows about this version information, the only place you could retrieve it from would be the registry.

Uninstall Registry Key :

The following installer properties give the values written under the registry key:

VersionMinor Derived from ProductVersion property

VersionMajor Derived from ProductVersion property

Version Derived from ProductVersion property

But I'd go with @devdigital's (implied) suggestion - you ought to have one of the assembly versions actually matching your installer version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM