在 MSBuild 中获取本机 exe 的文件版本 [英] Getting the file version of a native exe in MSBuild

查看:51
本文介绍了在 MSBuild 中获取本机 exe 的文件版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2010 解决方案中有许多 Visual C++ 项目.此解决方案中还有一个 WiX 项目,它为作为 C++ 项目之一的产品的可执行文件构建安装程序.

I have a number of Visual C++ projects in a Visual Studio 2010 solution. Also in this solution is a WiX project that builds the installer for the executable that is the product of one of the C++ projects.

可执行文件在其项目中有一个资源文件,该文件将程序的版本写入可执行文件.

The executable has a resource file in its project which writes the version of the program to the executable.

现在我想使用与资源文件写入可执行文件的编号相同的编号对 WiX 构建的安装程序进行版本控制.我在 StackOverflow 上搜索了 WiX 相关的帖子,找到了这篇帖子:

Now I'd like to version the WiX-built installer with the same number as the one written to the executable by the resource file. I've searched the WiX related posts on StackOverflow and found this post:

参考在 WiX 设置项目的 WiX 库项目中定义的 WixVariable

接受的答案似乎表明一个可能的解决方案是使用 MSBuild 和 BeforeBuild Target 中的 GetAssemblyIdentity 任务从另一个文件中获取版本号(在 SO 问题的情况下是一个 DLL,在我的情况下)可执行文件)并在 WiX 构建安装程序之前将其公开给 WiX.

The accepted answer to which, seems to indicate that a possible solution is to use MSBuild and the GetAssemblyIdentity task in the BeforeBuild Target to acquire the version number from another file (in the case of the SO question a DLL, in my case the executable) and expose it to WiX before WiX builds the installer.

我尝试将其添加到我的 .wixproj 文件的 MSBuild 部分,但是当我尝试构建安装程序时,我收到一条错误消息:

I tried adding this to the MSBuild portion of my .wixproj file but when I try to build the installer I get an error returned saying:

error MSB3441: Cannot get assembly name for "<ExePath>". Could not load file or assembly '<ExeName>.exe' or one of its dependencies. The module was expected to contain an assembly manifest.

我似乎无法在 MSDN 上找到有关此错误的任何信息,因为它与 MSBuild 相关.我已经检查了构建的可执行文件,它上面肯定有一个版本号(以及 .rc 文件中的其余信息),而 WiX 项目取决于输出可执行文件的项目;所以我假设它的 BeforeBuild 任务是在它所依赖的项目完全构建之后运行的.

I can't seem to find any information on MSDN about this error as it relates to MSBuild. I've checked the built executable and it definitely has a version number on it (as well as the rest of the information from the .rc file) and the WiX project depends on the project that outputs the executable; so I assume its BeforeBuild task is running after the project it depends on has been built completely.

我是否应该使用不同的任务而不是 GetAssemblyIdentity 来从 MSBuild 中的 .exe 中检索版本号,在 GetAssemblyIdentity 起作用之前是否还有其他要求需要满足,或者是否无法获取有关 .MSBuild 中的 exe 文件?

Should I be using a different task instead of GetAssemblyIdentity to retrieve the version number from the .exe in MSBuild, are there other requirements to satisfy before GetAssemblyIdentity will work, or is it just not possible to get this type of information about .exe files in MSBuild?

我接受了 Rob 的回答,因为我误解了 ProductVersion 和 FileVersion 之间的区别,而他建议的 WiX 技术正在按预期工作,并且是朝着我需要的解决方案迈出的一步.

I accepted Rob's answer since I was misunderstanding the difference between ProductVersion and FileVersion, and the WiX technique that he suggested was working as intended and is a step towards the solution I needed.

FileVersion 只是可执行文件的一个属性.Msi 文件本质上是数据库,而 ProductVersion 是该数据库中的一个条目;他们没有要设置的 FileVersion 属性.他建议的方法在 .msi 数据库中正确设置 ProductVersion.

FileVersion is an attribute of executables only. Msi files are essentially databases and ProductVersion is an entry in that database; they have no FileVersion attribute to set. The method he suggests correctly sets ProductVersion in the .msi database.

这个问题的标题现在与我实际遇到的问题没有真正的关系,因为我在寻求一个我认为我当时需要的解决方案.我现在已经解决了根本问题,即简单地访问安装程序的 ProductVersion.我在这里找到了一个在线发布的 cscript 脚本:http://kentie.net/article/wixnameversion/index.htm 显示如何访问 .msi 的 ProductVersion.使用它允许我提取 ProductVersion 并在其他工具中使用它.

The title of this question is now not really related to what the problem I actually had was, since I was asking for a solution I believed I needed at the time. I've now solved the root problem which was simply getting access to the ProductVersion of the installer. I found a cscript script posted online here: http://kentie.net/article/wixnameversion/index.htm that shows how to access the ProductVersion of the .msi. Using that has allowed me to extract the ProductVersion and use it in other tools.

推荐答案

如果您不需要 MSBuild 中的版本,一个更简单的解决方案是直接在 .wxs 文件中引用文件的版本.下面是一个片段,显示了要做什么:

An easier solution, if you don't need the version in MSBuild, is to just reference the version of the file directly in your .wxs file. Here's a snippet that shows what to do:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Version="!(bind.fileVersion.ExeWithVersion)" ...>

    ...

   <Component ...>
     <File Id="ExeWithVersion" Source="path\to\your\versioned\file.exe" />
   </Component> 

   ...

  </Product>
</Wix>

神奇之处在于 !(bind.fileVersion.Xxx) 说要使用 Id='Xxx' 查找 File 元素并获取它的版本.这是将文件版本放入 MSI 包的最简单方法.

The magic is that the !(bind.fileVersion.Xxx) says to look up the File element with Id='Xxx' and get its version. This is far away the easiest way to get the version of the file into your MSI package.

这篇关于在 MSBuild 中获取本机 exe 的文件版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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