如何使用C#以编程方式获取正在运行的程序的产品(而不是程序集)版本? [英] How do I programmatically get the product (not assembly) version of a running program using C#?

查看:97
本文介绍了如何使用C#以编程方式获取正在运行的程序的产品(而不是程序集)版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已编写的程序,正在尝试为其创建关于"框.我最近将程序的产品版本更新为1.00.0003,我希望它能反映在关于"窗口中.

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.

aboutBox的默认设置显示值为1.0.0.0,这是装配版本,而不是 product 版本.从那以后,我一直在Internet上搜寻以找到如何显示产品版本.我已经尝试了所有这些:

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;
    }
}

这些中的每一个都返回1.0.0.0(是的,我确实在控制台中查找了它们的输出,而不是实际显示的内容),而是像我所需要的那样返回1.00.0003.产品版本在InstallShield设置的常规信息"选项卡中设置.安装后,进入程序和功能"将显示产品版本为1.00.0003,因此我无法弄清楚为什么很难以编程方式检索此值.有什么想法吗?

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?

推荐答案

您的产品版本应与程序集版本匹配-请查看

Your product version should match the assembly version - have a look at How to make Product Version property match executable's version number automatically

这篇关于如何使用C#以编程方式获取正在运行的程序的产品(而不是程序集)版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆