wix - 获取父目录

时间:2012-08-21 09:32:15

标签: wix windows-installer

我的安装程序需要从注册表中读取值并将安装路径设置为该值的父级。

例如,从注册表中我得到:

D:\apps\client

然后安装程序应将应用程序安装到

D:\apps

我尝试[DIR]\..\(在“目录”或“CustomAction”中),但在安装时看到以下错误:

Error 1324. The folder path '..' contains an invalid character.

如何使用WiX执行此操作?

2 个答案:

答案 0 :(得分:3)

似乎你无法用纯wix做到这一点。您可以使用Custom Action Type 1。在“LaunchConditions”操作之前以立即模式执行它。在wix-code新属性中的某处初始化,如:

<Property Id="DIRFROMREG" Value="0" Secure="yes">  

以下是C#上的示例:

 public class CustomActions
{
    [CustomAction]
    public static ActionResult DirectorySearchAction(Session session)
    {
        try
        {
            session.Log("Directory search");
            RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"...your subkey...");
            if (reg != null)
            {
                var dir=reg.GetValue("...your value...");
                /*
                    var parentdir= split here your directory
                */
                session["DIRFROMREG"] =parentdir;
                session.Log(@"directory is ");
            }
            else
            {
                session.Log(@"The registry key is not found");
            }
        }
        catch (Exception e) 
        {
            session.Log(@"Error "+e);
        }
        return ActionResult.Success;
    }
}

最后一件事:

<SetProperty Id="INSTALLLOCATION" Value="[DIRFROMREG]" After="Your custom action">NOT DIRFROMREG=0</SetProperty>

希望这有帮助。

答案 1 :(得分:0)

Nerielle有一个很好的答案。我的安装全部都放在子文件夹中,所以当我找到一个旧组件时,我需要使用父文件夹进行安装,因此我来了here作为解答。
来自Custom action to manipulate property 我有了已知的固定安装子路径,因此找到了如何获取父文件夹。

    <!-- Set INSTALLFOLDER from SERVERINSTALLFOLDER without the \Server\ -->
    <CustomAction Id="VBScriptInstallFolderFromFoundServer" Script="vbscript">
      <![CDATA[         
        pathvalue = Session.Property("SERVERINSTALLFOLDER")
        if pathvalue <> "" Then
          Session.Property("INSTALLFOLDER") = Left(pathvalue,Len(pathvalue)-Len("\Server\"))
        End If
      ]]>
    </CustomAction>

结合Locate Installation directory of another product

    <Property Id="SERVERINSTALLFOLDER">
      <!-- Id="C_SERVER_SERVERHOST.EXE" Guid="{xxx GUID OF my exe component xxx}" -->
      <ComponentSearch Id="ServerComponentSearch" Type="file" Guid="{xxx GUID OF my exe component xxx}">
        <DirectorySearch Id="ServerComponentDirectorySearch" Depth="0" AssignToProperty="yes" />
      </ComponentSearch>
    </Property>

使用Wix remember property pattern 将INSTALLFOLDER路径存储在注册表中。
现在,我可以更新旧版本,或安装新版本以得到建议的以前安装的正确安装路径。
可以将vbscript更改为使用路径处理功能来查找父级,而不是删除固定的子字符串以更正确地回答问题,但是...
我的InstallUISequence和InstallExecuteSequence:

      <!-- Save INSTALLFOLDER parameter to CMDLINE_INSTALLFOLDER -->
      <Custom Action='SaveCmdLineValue' Before='AppSearch' />
      <!-- Set INSTALLFOLDER from SERVERINSTALLFOLDER without the \Server\ -->
      <Custom Action="VBScriptInstallFolderFromFoundServer" After="AppSearch">
        SERVERINSTALLFOLDER
      </Custom>
      <!-- Set INSTALLFOLDER from parameter CMDLINE_INSTALLFOLDER -->
      <Custom Action='SetFromCmdLineValue' After='VBScriptInstallFolderFromFoundServer'>
        CMDLINE_INSTALLFOLDER
      </Custom>

最后,在产品I中,要引用我将其放入的片段:

    <!-- Install to previous install path From parameter, OR from found installation OR from registry -->
    <CustomActionRef Id='SaveCmdLineValue' />
    <PropertyRef Id='INSTALLFOLDER'/><!-- include Fragment -->
    <PropertyRef Id='SERVERINSTALLFOLDER'/><!-- include Fragment -->
    <CustomActionRef Id='VBScriptInstallFolderFromFoundServer' /><!-- include Fragment -->