WiX – 复制任意文件



我的设置所在的文件夹.exe包含一个子文件夹,CAL具有名为xyz1234.cal的文件 - 它们的名称因客户而异。这些文件必须复制到目标目录中CAL文件夹中。因此,我创建了一个使用File.Copy()函数的自定义操作和 C# dll。我的 C# 函数接收字符串srcDirdestDir作为参数,例如 D:installationCALC:MyAppCAL .但是,当我使用 Directory.Exists(srcDir) 检查文件夹是否存在时,尽管目录D:installationCAL存在,但会抛出异常:

ERROR in custom action myFunction System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:WindowsInstallerMSID839.tmp-D:installationCAL'.

无论自定义是立即执行还是延迟执行,都会发生这种情况。 C:WindowsInstallerMSID839.tmp-似乎是已执行程序集的路径,但我当然不希望将其作为 FullPath 的一部分。我怎样才能摆脱它?

自定义操作和属性定义如下: <CustomAction Id='myCA' BinaryKey='myCABin' DllEntry='myFunction' Execute="deferred" HideTarget="no" Impersonate="no"/> <Property Id="myCA" Value="Arg1=[CURRENTDIRECTORY];Arg2=[INSTALLDIR]" />

参数的使用方式如下:

CustomActionData data = session.CustomActionData;
string srcDir = data["Arg1"]+ "\CAL";
string destDir = data["Arg2"]+ "\CAL";
if (Directory.Exists(srcDir))
    // copy files

我重新创建了您的应用程序,它工作正常。这是我的 wix 代码(它在我的产品节点内(:

<CustomAction Id='Test' BinaryKey='RegistryHelperCA' DllEntry='Test' Execute="deferred" HideTarget="no" Impersonate="no"/>
<Property Id="Test" Value="Arg1=[CURRENTDIRECTORY];Arg2=[INSTALLDIR]" />
<InstallExecuteSequence>
  <Custom Action="Test" After="InstallFiles"></Custom>
</InstallExecuteSequence>

我的自定义操作:

    [CustomAction("Test")]
    public static ActionResult Test(Session session)
    {
        string dir = session.CustomActionData["Arg1"];
        session.Log("DIRECTORY equals " + dir);
        if (Directory.Exists(dir))
            session.Log("Success");
        return ActionResult.Success;
    }

它吐出的目录C:UsersuserDesktop.验证您没有在任何地方分配给CURRENTDIRECTORY媒体资源,如果找不到任何内容,请尝试将自定义操作设置为Execute="immediate"并像这样访问数据

string srcDir = session["CURRENTDIRECTORY"]+ "\CAL";

如果这不起作用,那么该属性肯定在某处被覆盖。祝你好运!

经过一些试错会话后,我发现Directory.Exists(srcDir)Directory.Exists(destDir)不起作用,因为不是值而是属性名称作为参数传递给 Exist() 函数 - 与正确生成值的session.Log(srcDir)相反。

最后,我最终设置了execute="immediate"并检索了如下值:

srcDir = session["CURRENTDIRECTORY"];
destDir = session.GetTargetPath("INSTALLDIR");

最新更新