You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WIX Toolset MSI:如何从注册表读取注册表项(文件夹)名称?

获取注册表子项名称的解决方案

Windows Installer内置的<RegistrySearch>仅能读取注册表键值,无法直接获取注册表子项(即你所说的“文件夹”)的名称。你之前的写法错误,因为Name属性指定的是键值名称,而非子项名称。

要实现获取HKLM\SOFTWARE\Microsoft\PowerShell\3路径下PowerShellEngine子项的名称并赋值给POWERSHELLVERSION属性,需要通过自定义动作来完成,以下是两种可行方案:

方案一:使用PowerShell自定义动作

  1. 先定义目标属性:
<Property Id="POWERSHELLVERSION" Secure="yes" />
  1. 添加PowerShell执行的自定义动作(需确保项目引用了WixCommonExtension):
<!-- 配置要执行的PowerShell命令 -->
<CustomAction Id="SetPSCommand" Property="RunPSCommand" Value="&quot;powershell.exe&quot; -Command &quot;$parentKey = Get-Item 'HKLM:\SOFTWARE\Microsoft\PowerShell\3'; $targetSubKey = $parentKey.GetSubKeyNames() | Where-Object { $_ -eq 'PowerShellEngine' }; if ($targetSubKey) { Write-Host 'POWERSHELLVERSION=$targetSubKey' }&quot;" />
<!-- 执行PowerShell命令 -->
<CustomAction Id="RunPSCommand" BinaryKey="WixCA" DllEntry="WixQuietExec64" Execute="immediate" Return="check" />

<!-- 在安装序列中安排执行顺序 -->
<InstallExecuteSequence>
  <Custom Action="SetPSCommand" Before="RunPSCommand" />
  <Custom Action="RunPSCommand" Before="LaunchConditions" />
</InstallExecuteSequence>

方案二:使用VBScript自定义动作

  1. 同样先定义属性:
<Property Id="POWERSHELLVERSION" Secure="yes" />
  1. 添加VBScript自定义动作:
<CustomAction Id="GetSubKeyName" Script="vbscript" Execute="immediate">
<![CDATA[
Dim objReg, parentKeyPath, subKeyList, index
Set objReg = CreateObject("WScript.Shell")
parentKeyPath = "HKLM\SOFTWARE\Microsoft\PowerShell\3\"

On Error Resume Next
subKeyList = objReg.RegRead(parentKeyPath)
If Err.Number = 0 Then
  For index = LBound(subKeyList) To UBound(subKeyList)
    If subKeyList(index) = "PowerShellEngine" Then
      Session.Property("POWERSHELLVERSION") = subKeyList(index)
      Exit For
    End If
  Next
End If
On Error GoTo 0
]]>
</CustomAction>

<!-- 安排执行顺序 -->
<InstallExecuteSequence>
  <Custom Action="GetSubKeyName" Before="LaunchConditions" />
</InstallExecuteSequence>

说明

两种方案的核心逻辑都是通过脚本读取指定父注册表项的子项列表,筛选出目标子项名称后,将其赋值给POWERSHELLVERSION属性。Windows Installer会自动识别脚本中输出的属性名=值格式,完成属性赋值。

内容的提问来源于stack exchange,提问作者Alexandr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:15:42