首页 > 解决方案 > System.IO.FileInfo 在 WIX 自定义操作中使用时将意外字符串添加到路径

问题描述

我有一个WIX(V3.11.1)安装程序,我在其中创建一个基于传递给自定义操作的值的FileInfo 。传递给自定义操作的值是正确的,session.CustomActionData["INSTALLFOLDER"]返回正确的路径,即C:\Program Files(x86)\MyApplication.

不幸的是,当我创建时FileInfo targetDir = new FileInfo(session.CustomActionData["INSTALLFOLDER"]),结果targetDir.FullNameC:\Windows\Installer\MSIE335.tmp-\C:\Program Files(x86)\MyApplication\

我试图找到有关FileInfo构造函数如何工作的任何信息,但没有任何结果。您有什么想法为什么会C:\Windows\Installer\MSIE335.tmp-\出现在 FileInfo 中以及如何使用真实路径创建它?

我用来检查所有值的代码:

string path = session.CustomActionData["INSTALLFOLDER"];

session.Log(path); //result is C:\Program Files(x86)\MyApplication
FileInfo targetDir = new FileInfo(path);

session.Log(targetDir.FullName); // result is C:\Windows\Installer\MSIE335.tmp-\C:\Program Files(x86)\MyApplication\

标签: c#wixwindows-installer

解决方案


我的设置感觉猜测INSTALLFOLDERCustomActionData的值实际上是值[INSTALLFOLDER]。记录时,该语法将被解析为正确的值。这就是为什么它看起来不错。但是,FileInfo实际得到的是一个值,例如:

FileInfo targetDir = new FileInfo("[INSTALLFOLDER]");

这当然是“当前目录中名为“[INSTALLFOLDER]”的文件”。这与您的第二个日志行相匹配。

解决方法是确保您传递CustomActionData 中的值。 INSTALLFOLDER有几种不同的方法可以做到这一点,具体取决于您如何安排延迟的自定义操作和设置命名属性。例如,使用SetProperty应该是修复它的简单方法。

更新:Hawex 提供了一个定义自定义操作的片段。它看起来像:

<Property Id="CustomActionOnInstall" Value="INSTALLFOLDER=[INSTALLFOLDER]" />

<CustomAction Id="CustomActionOnInstall" BinaryKey="CustomActions" Execute="deferred" 
              Impersonate="no"  DllEntry="OnInstall" Return="check" />
<InstallExecuteSequence>
  <Custom Action="CustomActionOnInstall" Before="InstallFinalize">NOT Installed</Custom>      
</InstallExecuteSequence>

要修复,只需将静态(未评估)更改PropertySetProperty

<SetProperty Id="CustomActionOnInstall" Value="INSTALLFOLDER=[INSTALLFOLDER]"
             Before="CustomActionOnInstall" Sequence="execute" />

推荐阅读