首页 > 解决方案 > 根据安装程序是处于用户模式还是管理员模式更改环境注册表项

问题描述

如果用户以管理员模式运行安装程序,则应修改系统路径,如果安装程序以用户模式运行,则需要修改用户环境变量。

[Registry]

; If user installation mode
#define EnvironmentRootKey "HKCU"
#define EnvironmentKey "Environment"
; If admin mode
#define EnvironmentRootKey "HKLM"
#define EnvironmentKey "System\CurrentControlSet\Control\Session Manager\Environment"

Root: {#EnvironmentRootKey}; Subkey: "{#EnvironmentKey}"; ValueType: expandsz; \
  ValueName: "Path"; ValueData: "{olddata};{app}\bin"; Tasks: addtopath; \
  Check: NeedsAddPath(ExpandConstant('{app}\bin'))

如果安装程序处于用户模式和管理员模式,我知道HKA会自动解析,但.HKCUHKLMEnvironmentKey

基本上是这样的:

#if "HKA" == "HKCU"
#define EnvironmentKey "Environment"
#else
#define EnvironmentKey "System\CurrentControlSet\Control\Session Manager\Environment"
#endif

标签: windowsinstallationregistryinno-setup

解决方案


使用脚本常量

[Registry]
Root: HKA; Subkey: "{code:GetEnvironmentKey}"; ...
[Code]
function GetEnvironmentKey(Param: string): string;
begin
  if IsAdminInstallMode then
    Result := 'System\CurrentControlSet\Control\Session Manager\Environment'
  else
    Result := 'Environment';
end;

推荐阅读