【发布时间】:2014-11-24 03:38:33
【问题描述】:
我一直在尝试使用 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
然后再次运行,它可以工作。这里有什么问题?
【问题讨论】:
-
对不起,但对我来说不清楚,你在尝试什么。特别是,您对句子“一切正常......使用上面给出的命令。[...]但是当我使用相同的(???)命令时,......”这句话的意思。第二:您要做什么,只需将命令行中给出的 ip 和端口记录到另一个配置文件中?或者您想从配置文件中取出两个 msi 属性?第三,也是最重要的。 msi 日志文件说的是您的自定义操作是否已启动。没有这些信息,我们什么也说不出来。
-
程序编译的dll用于Wix的Custom Action内部,它应该获取给msifile的参数并将这些参数写入某个配置文件。当我使用上面的命令运行 msi 文件时,它会正确完成所有工作并创建配置文件。当我第二次运行相同的命令等时,不会获取这些参数并且配置文件看起来像 {"ip:"","port":""} 。如果我使用下面给出的命令卸载 msifile 并安装它再次,它确实有效。我的代码有问题吗。
-
@SarvagyaPant 您不能一遍又一遍地执行相同的安装程序,MSI 将检测到它当前已安装并中止安装。如果我没记错的话,您正在考虑维修或升级。如果是这种情况,您需要配置安装程序以使其正常运行。
标签: c# wix windows-installer