我在 WiX 中制作 MSI 安装程序。在安装过程中,我想从自定义操作运行可执行文件并获取其标准输出(而不是返回代码)以供以后在安装过程中使用(Property
据说是元素)。
如何在 WiX (3.5) 中实现它?
我将此代码用于类似的任务(它是 C# DTF 自定义操作):
// your process data
ProcessStartInfo processInfo = new ProcessStartInfo() {
CreateNoWindow = true,
LoadUserProfile = true,
UseShellExecute = false,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
...
};
Process process = new Process();
process.StartInfo = processInfo;
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
if (!string.IsNullOrEmpty(e.Data) && session != null)
{
// HERE GOES THE TRICK!
Record record = new Record(1);
record.SetString(1, e.Data);
session.Message(InstallMessage.ActionData, record);
}
};
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
if (process.ExitCode != 0) {
throw new Exception("Execution failed (" + processInfo.FileName + " " + processInfo.Arguments + "). Code: " + process.ExitCode);
}
process.Close();
这被称为“屏幕抓取”,虽然在技术上可以创建基础架构以在进程外运行 EXE,抓取它的输出,然后将数据编组回 MSI 上下文,但它永远不会是一个强大的解决方案。
更好的解决方案是了解 EXE 的作用以及它的作用方式。然后编写一个 C# o C++ 自定义操作,该操作在进程中运行并可以访问 MSI 句柄,以便您可以完成工作并设置您需要设置的属性。