1

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

aboutBox 的默认设置显示值 1.0.0.0,这是程序集版本,而不是产品版本。从那以后,我一直在互联网上寻找如何获得要显示的产品版本。我已经尝试了所有这些:

{
    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 的产品版本,所以我无法弄清楚为什么以编程方式检索该值如此困难。有任何想法吗?

4

3 回答 3

2

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

于 2013-02-22T07:15:53.193 回答
1

您要检索的版本 1.00.0003 是您的产品安装程序的版本。要以编程方式获取此版本,您需要检查安装程序(MSI 文件),而不是已安装的文件。我不确定这是否是您想要做的,但有一个问题的答案Checking ProductVersion of an MSI programatically解释了如何做到这一点。

如果您希望您的可执行文件包含相同的版本号,您需要以某种方式使用 .NET 属性AssemblyFileVersion或 WindowsVERSIONINFO资源来存储版本号。

InstallShield 允许您在命令行上指定产品版本。这允许您将产品版本存储在单个文件中,然后将其用作安装程序中嵌入的产品版本以及AssemblyFileVersion程序集的来源。

于 2013-02-22T07:22:30.930 回答
0

如果只有安装程序知道此版本信息,那么您可以从中检索它的唯一地方就是注册表。

卸载注册表项

以下安装程序属性给出了注册表项下写入的值:

VersionMinor 派生自 ProductVersion 属性

VersionMajor 派生自 ProductVersion 属性

从 ProductVersion 属性派生的版本

但我会接受@devdigital 的(暗示的)建议——你应该有一个实际匹配你的安装程序版本的程序集版本。

于 2013-02-22T07:23:21.433 回答