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

查看:188
本文介绍了运行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 。正如 [注册表] 部分的Inno安装文档所述,有一种方法可以将元素附加到:

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:


字符串 expandingz multisz 类型值您可以在此参数中使用一个名为 {olddata} 的特殊常量。 {olddata} 替换为注册表值的先前数据。如果您需要将字符串附加到现有值, {olddata} 常量可能很有用,例如 {olddata}; {app} / code>。如果该值不存在或现有值不是字符串类型,则 {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: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
    ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};C:\foo"

这将附加C: \\ foo目录到路径。

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

不幸的是,第二次安装时会重复,这也应该是固定的。 a 使用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: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
    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,
    'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
    '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天全站免登陆