1

现在,在我们的一个 WiX 安装程序中,我们明确定义了产品版本字符串,如下所示:

<?define ProductVersion="1.2.3"?>

我们还在另一个表单的标题中使用了相同的版本号,下面是一个非常简化的示例,说明了如何应用:

public partial class frmMain : Form
{
    // assume the designer code is all properly generated

    private const string VERSION = "1.2.3";

    public frmMain()
    {
        InitializeComponent();
        this.Text += string.Format(" v{0}", VERSION);
    }
}

这看起来很笨拙,我觉得没有必要在两个地方更新我们的产品版本。存储版本字符串信息的最佳位置在哪里,所以我只需在一个地方更新它,并且只需从表单和安装程序中引用这些数据?

请注意,在这种情况下,产品版本与程序集版本不匹配。

4

2 回答 2

5

C# 没有 C/C++ 和 WiX 工具集那样的预处理器,因此您不能做显而易见的事情并通过该路径传递版本。如果产品版本与程序集文件版本匹配,那么您可以执行以下操作:

<Product Version='!(bind.fileVersion.FileIdOfAssembly)'>

如果文件版本可以匹配,那将是理想的。如果没有,那么剩下的唯一选择就是在安装时写一些东西并在运行时读取它。例如:

<RegistryValue Root='HKLM'
               Path='SOFTWARE\!(bind.property.Manufacturer)\!(bind.property.ProductName)'
               Name='Version' Value='!(bind.property.ProductVersion)' Type='string' />

然后在您的 frmMain() 中读取该注册表项。不确定是否值得增加应用程序的复杂性,因为您现在拥有简单、强大的解决方案。

于 2013-04-13T05:32:43.537 回答
1

I am not sure you would like to go through as much trouble as my solution will require, but here is it:

First, you should store assembly version in AssemblyInfo.cs file. This will allow sharing version (and other company - specific info) between projects just by referencing a common AssemblyInfo in all your projects. You can do it by adding existing file to a project as a link. For example, all our projects have two AssemblyInfo files: one local, project specific (GUID, etc...), and one common, with version info and company name.

[assembly: AssemblyFileVersion("1.3.100.25")]
[assembly: AssemblyVersion("1.1.0.0")]

Second, if you have not done this already, take the WIX version out of WXS file and put it into a separate WXI file. Again, this will allow separate editing of version (and other constants, if needed), and referencing it in several projects:

<?include ..\..\..\Common\WIX\Version.wxi ?>

Then, you will have to write a build task for MSBuild, and incorporate it as a pre-build dependency for all projects. In the build task, you can take version number from WXI file and put it into AssemblyInfo file, or vice versa. You can even store version data in a separate XML and inject it into both WXI and AssemblyInfo. Reading and writing WXI and AssemblyInfo is a simple string manipulation in C#, do not bother yourself with Reflection and stuff.

This third step is the only required one, and the most difficult. You should probably do all this if you have a lot of projects, or using automated builds.

于 2013-04-14T07:28:31.570 回答