问题描述
我使用 WiX 玩了几个星期,如果不是 CD-ROM 驱动器,我想将文件安装到 D:-驱动器。否则必须安装到 C:。
我创建了此自定义操作以查看驱动器是否已修复并准备就绪:
[CustomAction]
public static ActionResult CheckDataDrive(Session session)
{
ActionResult retVal;
session.Log("Begin CheckDataDrive");
if (TrySetTargetDir("D",session))
{
retVal = ActionResult.Success;
}
else if (TrySetTargetDir("C",session))
{
retVal = ActionResult.Success;
}
else
{
retVal = ActionResult.Failure;
}
session.Log("End CheckDataDrive");
return retVal;
}
private static bool TrySetTargetDir(string driveLetter,Session session)
{
var driveInfo = new DriveInfo(driveLetter);
if (driveInfo.DriveType != DriveType.Fixed || !driveInfo.IsReady)
return false;
// Set the INSTALLFOLDER
session["INSTALLFOLDER"] = session["INSTALLFOLDER"].Replace(session["TARGETDIR"],$"{driveLetter}:\\");
session.Log($"INSTALLFOLDER changed to {session["INSTALLFOLDER"]}");
return true;
}
这就是我在 Wix 产品中所拥有的:
<Feature Id="ProductFeature" Title="SetupGatewayFiles" Level="1">
<ComponentGroupRef Id="ConfigFiles" />
</Feature>
<InstallExecuteSequence>
<!-- Sets TARGETDIR to either D:\ or C:\ -->
<Custom Action='CheckDrive' After="CostFinalize" />
</InstallExecuteSequence>
剩下的就在这个片段中:
<Fragment>
<Binary Id="CustomActionBinary" SourceFile="$(var.CustomAction1.TargetDir)$(var.CustomAction1.TargetName).CA.dll" />
<CustomAction Id="CheckDrive" BinaryKey="CustomActionBinary" DllEntry="CheckDataDrive" Return="check" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="INSTALLFOLDER" Name="Configurations" />
</Directory>
<ComponentGroup Id="ConfigFiles" Directory="INSTALLFOLDER">
<Component Guid="{C5AF7E80-4D59-47CD-A537-2BC4BE90FDE3}" Id="ProductComponent" >
<File Source ="Application.xml" />
</Component>
</ComponentGroup>
</Fragment>
MSI (s) (14!B4) [18:49:46:510]:属性更改:修改 INSTALLFOLDER 属性。它的当前值为“D:\Configurations”。它的新值:'c:\Configurations'。
属性(S):INSTALLFOLDER = c:\Configurations\
但该文件仍然安装在 D:\Configurations 中!我做错了什么?
解决方法
After="CostFinalize" 似乎为时已晚,无法安排此类行动。 您可以尝试将其替换为:
<Custom Action='CheckDrive' Before='CostInitialize'>
,
尝试罗伯特的回答后,我发现 TARGETDIR 在 CostFinalize 之前(因此在 CostInitialize 之前)没有初始化,所以我得到一个异常 TARGETDIR 为空。在 CostFinalize 之后为时已晚。所以我尝试了一种不同的方法:我不想重用部分属性 TARGETDIR 和 INSTALLFOLDER,所以我完全硬编码了 de CustomAction 中的路径。我按照罗伯特的解决方案更早地设置了值。所以这就是我最终的结果:
private static bool TrySetTargetDir(string driveLetter,Session session)
{
var driveInfo = new DriveInfo(driveLetter);
if (driveInfo.DriveType != DriveType.Fixed || !driveInfo.IsReady)
return false;
// Set the INSTALLFOLDER
session["INSTALLFOLDER"] = $@"{driveLetter}:\Configurations";
session.Log($"INSTALLFOLDER changed to {session["INSTALLFOLDER"]}");
return true;
}
并在序列中较早地执行自定义操作:
<Custom Action='CheckDrive' Before='CostInitialize'>
并将 INSTALLFOLDER 设置为不可用的值:
<Directory Id="INSTALLFOLDER" Name="tbdInCustomAction" />