首次运行后Wix安装程序的自定义操作失败

时间:2014-11-24 03:38:34

标签: c# wix msiexec

我一直在尝试使用msiexec实现msi安装并将自定义参数传递给它。

msiexec /i somefile.msi /l*v output.txt IPADDRESS="127.0.0.1" PORT="9999"

现在我有以下代码来完成获取IPADDRESS和PORT的工作并将它们写入文件。以下是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;

namespace SetupCA
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult WriteFileToDisk(Session session)
        {
            session.Log("Begin WriteFileToDisk");

            string ipAddress = session["IPADDRESS"];
            string port = session["PORT"];
            string temp = @"
{{
 ""ip"" : ""{0}"" ,
""port"" : ""{1}""
}}";
            string config = string.Format(temp, ipAddress, port);
            session.Log("Config Generated was " + config);  
            System.IO.Directory.CreateDirectory("C:\\somefolder");
            try{
                 System.IO.File.Delete("C:\\somefolder\\some.config");
            }
            catch(Exception e){

            }
            System.IO.File.WriteAllText(@"C:\somefolder\some.config", config); 
            session.Log("Ending WriteFileToDisk");

            return ActionResult.Success;
        }
    }
}

编辑:完整的Wix代码 我使用了在Wix中生成的dll文件:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="CustomWixInstallerWithCustomAction" Language="1033" Version="1.0.0.0" Manufacturer="Developer" UpgradeCode="ba9015b9-027f-4451-adb2-e38f9168a850">
    <Package InstallerVersion="200" Compressed="no" InstallScope="perMachine" />

    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />

    <Feature Id="ProductFeature" Title="CustomWixInstallerWithCustomAction" Level="1">
        <ComponentGroupRef Id="ProductComponents" />
    </Feature>
</Product>

<Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
        <Directory Id="ProgramFilesFolder">
            <Directory Id="INSTALLFOLDER" Name="CustomWixInstaller" />
        </Directory>
    </Directory>
</Fragment>

<Fragment>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
  <Component Id="SomeRandomEXE">
    <File Source ="some.exe" />
  </Component>
    </ComponentGroup>
<Binary Id="SetupCA"  SourceFile="SetupCA.CA.dll"/>
<CustomAction Id="WRITEFILETODISK" Execute="immediate" BinaryKey="SetupCA" DllEntry="WriteFileToDisk" />
<InstallExecuteSequence>
  <Custom Action="WRITEFILETODISK" Sequence="2"></Custom>

</InstallExecuteSequence>
</Fragment>

使用上面给出的命令安装msi时,一切正常。文件文件在文件夹中生成,其中包含参数期间给出的内容。但是当我使用相同的命令时,这些参数不会被提取并写入文件中。但如果我使用以下方法卸载它:

msiexec /x file.msi

再次运行,它有效。这有什么问题?

2 个答案:

答案 0 :(得分:1)

Rolo在评论中首先给出了正确答案:

更详细:
如果您第二次调用msi设置,则不能指望它与第一次执行相同操作。 首先调用(如果一切顺利),它会进行安装,第二次调用,它几乎不执行任何操作,并认识到产品已经安装。

在两者之间卸载或使用&#34; REINSTALL = ALL&#34;参数(以及其他选项,如REINSTALLMODE,也许),然后修复/重新安装完成。

即使您的自定义操作适用于&#34;第二个电话&#34;没什么值得的。这就像试图破坏墙上的灯开关。有人可以说:&#34;我已经将开关连接到Arduino,每次有人打开灯时,都会发出警报声。但不幸的是,如果我在灯仍然亮的时候第二次点亮灯开关,它就不起作用......&#34;

希望这个例子有助于展示MSI背后的想法。

- 此外:

对于您想要的,经验丰富的msi人员不会使用自定义操作。 &#34; IniFile表&#34;是为了这样的事情。对于整个MSI空间而言,内置特征可能比自定义动作更可靠,仅允许一个示例也用于延迟的自定义动作,例如,某些安全方案中必需的。简单的自定义操作无法轻松读取此类场景中的属性。

http://msdn.microsoft.com/en-us/library/aa369282%28v=vs.85%29.aspx

http://blogs.technet.com/b/alexshev/archive/2008/04/03/from-msi-to-wix-part-15-installable-items-ini-files.aspx

答案 1 :(得分:0)

如果没有看到整个WiX文件,很难告诉你出了什么问题。有许多元素可能会阻止它。但从听起来来看,我不认为自定义操作会在重载安装的情况下触发。

在我对你这个问题的另一个问题的另一个答案中,我建议:

        session.Log("Begin WriteFileToDisk");

在customAction中。这正是调试的原因。在第二次运行安装程序时,您是否在输出日志中看到这些行(对于Begin和End)?

最后,一个可能对您有帮助的工具将是ORCA.exe工具     http://msdn.microsoft.com/en-us/library/aa370557%28v=vs.85%29.aspx

该工具可让您打开msi文件,并查看已设置的所有配置。因此,您可以查看是否为您的自定义操作设置了条件。

相关问题