繁体   English   中英

Wix中的自定义操作以操作字符串和设置属性

[英]Custom Action in Wix to manipulate string and set property

嗨,我正在使用Wix创建安装程序,该安装程序必须使用安装程序在用户系统上复制的文件的路径写入注册表值。 问题是注册表项应以这种格式编写

file:///C:/Program Files/....

在Wix代码项目中,我有INSTALLFOLDER目录ID,它指向

C:\Program Files\....

我真的很难将后一种表示法转换成前一种表示法。 我创建了一个自定义操作,希望设置一个属性,以便可以使用它。 以下是代码

自定义操作(目前为单独的DLL,可以内联吗?)

 public class CustomActions
    {
        [CustomAction]
        public static ActionResult CustomAction1(Session session)
        {
            session.Log("Begin CustomAction1");
            string origValue = session["INSTALLFOLDER"];
            MessageBox.Show(origValue);
            string retVal = origValue.Replace("\\", "//");
            MessageBox.Show(retVal);
            session["Custom_Prop"] = retVal;
            return ActionResult.Success;
        }
    }

和Product.wxs

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">

  <Product Id="*" Name="SetupProject1" Language="1033" Version="1.2.0.0" Manufacturer="nik" UpgradeCode="4a74ff86-49a9-4011-9794-e1c18077172f">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />

    <Feature Id="ProductFeature" Title="SetupProject1" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>

    <InstallExecuteSequence>
      <Custom Action='FooAction' Before='InstallFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

  </Product>

  <Fragment>
    <CustomAction Id='FooAction' BinaryKey='FooBinary' DllEntry='CustomAction1' Execute='immediate'
                  Return='check'/>

    <Binary Id='FooBinary' SourceFile='MyCustomAction.CA.dll'/>
  </Fragment>


  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="SetupProject1" />
      </Directory>
    </Directory>
  </Fragment>

  <Fragment>
    <Property Id="Custom_Prop" Value="[ProgramFilesFolder]"></Property>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->
      <!-- <Component Id="ProductComponent"> -->
      <!-- TODO: Insert files, registry keys, and other resources here. -->
      <!-- </Component> -->
      <Component Id="cmp_Add_Mainfest_To_Registry" Guid="955A3A76-F010-4FCB-BCAF-B297AFD1C05B">

        <RegistryKey Root="HKCU" Key="SOFTWARE\company">

          <RegistryValue Name="LoadBehavior" Value="3" Type="integer" Action="write" />

          <RegistryValue Name="Manifest" Value="[Custom_Prop]" Type="string" Action="write"  KeyPath="yes"/>
        </RegistryKey>
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

但是,当我运行此安装程序时,注册表中写入的值是文字字符串[ProgramFolder],而不是对C:\\或C:/的求值

有人可以帮忙吗?

我的代码无法正常工作的原因是这一行

 <InstallExecuteSequence>
      <Custom Action='FooAction' Before='InstallFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

在如下更改前属性的值时,可以完成此工作

<InstallExecuteSequence>
      <Custom Action='FooAction' Before='CostFinalize'>NOT Installed</Custom>
    </InstallExecuteSequence>

但是,由于我的需求非常简单,所以我决定不为CustomAction使用单独的DLL,而是在Wix Project中的vbscript中进行了Custom Action。 所以现在代码看起来像

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">

  <Product Id="*" Name="SetupProject1" Language="1033" Version="1.3.1.0" Manufacturer="nik" UpgradeCode="4a74ff86-49a9-4011-9794-e1c18077172f">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate />

    <Feature Id="ProductFeature" Title="SetupProject1" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>

     <InstallExecuteSequence>
      <Custom Action="VBScriptCommand" After="CostFinalize">NOT REMOVE</Custom>
    </InstallExecuteSequence>

  </Product>

  <Fragment>


    <CustomAction Id="VBScriptCommand" Script="vbscript">
      <![CDATA[         
    value = Session.Property("INSTALLFOLDER")
    origPath=Session.Property("INSTALLFOLDER")
    If Right(webdir, 1) = "\" Then
      value = Left(value, Len(value) - 1) 
    End If

    Session.Property("SOME_PROPERTY") = Replace(origPath,"\","//")      
  ]]>
    </CustomAction>

   </Fragment>

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="SetupProject1" />
      </Directory>
    </Directory>
  </Fragment>

  <Fragment>
    <Property Id="Custom_Prop" Value="[ProgramFilesFolder]"></Property>
    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">

      <Component Id="cmp_Add_Mainfest_To_Registry" Guid="955A3A76-F010-4FCB-BCAF-B297AFD1C05B">

        <RegistryKey Root="HKCU" Key="SOFTWARE\something">

          <RegistryValue Name="LoadBehavior" Value="3" Type="integer" Action="write" />
          <!--<RegistryValue Name="Manifest" Value="[#FILE_VstoManifest]|vstolocal" Type="string" Action="write" />-->
          <RegistryValue Name="Manifest" Value="file:///[SOME_PROPERTY]" Type="string" Action="write"  KeyPath="yes"/>
        </RegistryKey>
      </Component>
    </ComponentGroup>
  </Fragment>
</Wix>

也许纯粹主义者不会这样,但是为什么要使用Shot Shot枪杀死苍蝇呢?

尼基尔(Nikhil)帮助了我。 我的安装全部都放在子文件夹中,所以当我找到一个旧组件时,我需要使用父文件夹进行安装,因此我来这里寻求答案。
结合此获取父项,我找到了如何获取父文件夹,因为我有一个已知的固定安装子路径。

    <!-- 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>

另一个产品的“定位安装”目录结合

    <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记住属性模式,将INSTALLFOLDER路径存储在注册表中。
现在,我可以更新旧版本,或安装新版本以获取先前安装的正确安装路径作为建议。
这不是问题的答案,但是当我被带到这里时,我的答案会帮助其他人走同样的道路...
我的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 -->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM