运行 Inno Setup Installer 时如何修改 PATH 环境变量? [英] How do I modify the PATH environment variable when running an Inno Setup Installer?

查看:65
本文介绍了运行 Inno Setup Installer 时如何修改 PATH 环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Inno Setup 允许您通过 [Registry] 部分设置环境变量(通过设置与环境变量对应的注册表项)

Inno Setup lets you set environment variables via the [Registry] sections (by setting registry key which correspond to environment variable)

但是,有时您不只是想设置一个环境变量.通常,您想对其进行修改.例如:在安装时,可能需要在 PATH 环境变量中添加/删除目录.

However, sometimes you don't just wanna set an environment variable. Often, you wanna modify it. For example: upon installation, one may want to add/remove a directory to/from the PATH environment variable.

如何在 InnoSetup 中修改 PATH 环境变量?

How can I modify the PATH environment variable from within InnoSetup?

推荐答案

您提供的注册表项中的路径是 REG_EXPAND_SZ 类型的值.正如 [Registry] 部分的 Inno Setup 文档所述,有一种方法可以将元素附加到这些:

The path in the registry key you gave is a value of type REG_EXPAND_SZ. As the Inno Setup documentation for the [Registry] section states there is a way to append elements to those:

stringexpandszmultisz 类型值上,您可以使用称为 {olddata} 在这个参数中.{olddata} 替换为注册表值的先前数据.如果您需要将字符串附加到现有值,例如 {olddata};{app}{olddata} 常量会很有用.如果值不存在或现有值不是字符串类型,则 {olddata} 常量将被静默删除.

On a string, expandsz, or multisz type value, you may use a special constant called {olddata} in this parameter. {olddata} is replaced with the previous data of the registry value. The {olddata} constant can be useful if you need to append a string to an existing value, for example, {olddata};{app}. If the value does not exist or the existing value isn't a string type, the {olddata} constant is silently removed.

因此,可以使用与此类似的注册表部分附加到路径:

So to append to the path a registry section similar to this may be used:

[Registry]
Root: HKLM; Subkey: "SYSTEMCurrentControlSetControlSession ManagerEnvironment"; 
    ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};C:foo"

这会将C:foo"目录附加到路径中.

which would append the "C:foo" directory to the path.

不幸的是,当您第二次安装时,这会重复,这也应该修复.Check 参数与 Pascal 脚本中编码的函数可用于检查路径是否确实需要扩展:

Unfortunately this would be repeated when you install a second time, which should be fixed as well. A Check parameter with a function coded in Pascal script can be used to check whether the path does indeed need to be expanded:

[Registry]
Root: HKLM; Subkey: "SYSTEMCurrentControlSetControlSession ManagerEnvironment"; 
    ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};C:foo"; 
    Check: NeedsAddPath('C:foo')

此函数读取原始路径值并检查给定目录是否已包含在其中.为此,它会在路径中添加用于分隔目录的分号字符.考虑到搜索目录可能是第一个或最后一个元素的事实,分号字符也被预先添加并附加到原始值:

This function reads the original path value and checks whether the given directory is already contained in it. To do so it prepends and appends semicolon chars which are used to separate directories in the path. To account for the fact that the searched for directory may be the first or last element semicolon chars are prepended and appended to the original value as well:

[Code]

function NeedsAddPath(Param: string): boolean;
var
  OrigPath: string;
begin
  if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
    'SYSTEMCurrentControlSetControlSession ManagerEnvironment',
    'Path', OrigPath)
  then begin
    Result := True;
    exit;
  end;
  { look for the path with leading and trailing semicolon }
  { Pos() returns 0 if not found }
  Result := Pos(';' + Param + ';', ';' + OrigPath + ';') = 0;
end;

请注意,在将常量作为参数传递给检查函数之前,您可能需要展开常量,详情请参阅文档.

Note that you may need to expand constants before you pass them as parameter to the check function, see the documentation for details.

在卸载期间从路径中删除此目录可以以类似的方式完成,留给读者作为练习.

Removing this directory from the path during uninstallation can be done in a similar fashion and is left as an exercise for the reader.

这篇关于运行 Inno Setup Installer 时如何修改 PATH 环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆