问题描述
在我们的构建过程中,目前有可能将非代码文件(例如图像文件)添加到我们的 Web 项目中,但不包含在 WiX 构建的 MSI 安装程序中.
In our build process there is currently the potential for non-code based files (such as image files) to be added to our web project, but not included in the MSI installer built by WiX.
为了防止出现这种情况,我想在我们的 WiX 项目的 AfterBuild 目标中执行以下操作:
To help prevent this, I want to perform the following in the AfterBuild target for our WiX project:
- 获取构建的所有文件的计数(Web 部署项目的输出)
- 获取 MSI 中内置的所有文件的数量(来自 MSI 中的文件"表)
- 比较计数,如果不匹配则构建失败
如果我启动 Orca,我可以很容易地看到文件表和计数,但我不知道如何从 MSBuild 自动执行此操作.是否有一些 API 或其他机制可以从 MSI 中获取此信息?
If I fire up Orca I can easily see the File table and count, but I don't know how to automate this from MSBuild. Is there some API or other mechanism to get this information out of an MSI?
我不介意编写自定义 MSBuild 任务来提取 MSI 文件表计数.
I don't mind writing a custom MSBuild task to extract the MSI File table count.
推荐答案
新建一个visual studio项目,添加对c:\windows\system32\msi.dll
的引用并使用以下代码读取 msi 文件中的文件数:
Create a new visual studio project, add a reference to c:\windows\system32\msi.dll
and use the following code to read the number of files in a msi file:
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
var installer =
(WindowsInstaller.Installer)Activator.CreateInstance(installerType);
var msi = installer.OpenDatabase(@"path\to\some\file.msi", 0);
var fileView = msi.OpenView("SELECT FileName FROM File");
fileView.Execute(null);
int fileCount = 0;
while (fileView.Fetch() != null)
{
fileCount++;
}
Console.WriteLine(fileCount);
此代码使用 WindowsInstaller.Installer
COM 对象,它是 Windows 安装程序自动化 API 的入口点.查看完整参考文档.
This code uses the WindowsInstaller.Installer
COM object, which is the entry-point for the windows installer automation API. Take a look at the complete reference documentation.
edit:显然 wix 带有托管程序集(在 C:\program files\Windows Installer XML v3\sdk
中),它包装了 msi.dll.我想这就是 Rob 在他的回答中所指的DTF".使用 Microsoft.Deployment.WindowsInstaller 程序集中的类型和命名空间将代码示例简化为:
edit: apparently wix comes with managed assemblies (in C:\program files\Windows Installer XML v3\sdk
) which wrap msi.dll. I guess this is what Rob is referring to by "DTF" in his answer. Using the types in the Microsoft.Deployment.WindowsInstaller assembly and namespace would simplify the code sample to this:
var database = new Database(@"\path\to\some\file.msi");
var list = database.ExecuteQuery("SELECT FileName FROM File");
Console.WriteLine(list.Count);
这篇关于如何从MSI“文件"中提取数据(文件计数)桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!