运行 Inno Setup 安装程序时如何修改 PATH 环境变量?
Inno Setup 允许您通过 [Registry] 部分设置环境变量(通过设置与环境变量相对应的注册表项)
但是,有时您不仅仅想设置环境变量。通常,您想修改它。例如:安装时,人们可能想要向 PATH 环境变量添加/删除目录。
如何从 InnoSetup 中修改 PATH 环境变量?
Inno Setup lets you set environment variables via the [Registry] sections (by setting registry key which correspond to environment variable)
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.
How can I modify the PATH environment variable from within InnoSetup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您提供的注册表项中的路径是
REG_EXPAND_SZ
类型的值。正如 [Registry] 部分的 Inno Setup 文档所述,有一种方法可以将元素附加到这些元素:因此,要附加到路径,可以使用与此类似的注册表部分:
它将“C:\foo”目录附加到路径。
不幸的是,当您第二次安装时,这会重复出现,这也应该得到修复。带有 Pascal 脚本中编码的函数的
Check
参数可用于检查路径是否确实需要扩展:该函数读取原始路径值并检查给定目录是否已包含在其中。为此,它会在前面和后面添加分号字符,这些分号字符用于分隔路径中的目录。考虑到搜索的目录可能是第一个或最后一个元素,分号字符也被前置并附加到原始值:
请注意,在将常量作为参数传递给检查函数之前,您可能需要扩展常量,请参阅有关详细信息,请参阅文档。
在卸载期间从路径中删除此目录可以以类似的方式完成,并留给读者作为练习。
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:So to append to the path a registry section similar to this may be used:
which would append the "C:\foo" directory to the path.
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: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:
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.
我遇到了同样的问题,但尽管有上面的答案,我最终还是得到了一个自定义解决方案,我想与您分享。
首先,我使用两种方法创建了
environment.iss
文件 - 一种用于添加环境的 Path 变量的路径,第二种用于删除它:参考:
RegQueryStringValue
,RegWriteStringValue
现在,在主 .iss 文件中,我可以包含此文件并监听 2事件(有关事件的更多信息,您可以在文档中的事件函数部分了解) 、
CurStepChanged
在安装后添加路径,以及CurUninstallStepChanged
在用户卸载应用程序时将其删除。在下面的示例脚本中添加/删除bin
目录(相对于安装目录):参考:
ExpandConstant
注意#1:仅安装步骤添加路径一次(确保安装的可重复性)。
注意#2:卸载步骤仅从变量中删除一次出现的路径。
奖励:安装步骤带有复选框“添加到路径变量”。
要使用复选框“添加到 PATH 变量” 添加安装步骤,请在
[Tasks]
部分定义新任务(默认选中):然后你可以在
CurStepChanged
事件中检查它:I had the same problem but despite the answers above I've ended up with a custom solution and I'd like to share it with you.
First of all I've created the
environment.iss
file with 2 methods - one for adding path to the environment's Path variable and second to remove it:Reference:
RegQueryStringValue
,RegWriteStringValue
Now in main .iss file I could include this file and listen for the 2 events (more about events you can learn in Event Functions section in documentation),
CurStepChanged
to add path after installation andCurUninstallStepChanged
to remove it when user uninstall an application. In below example script add/remove thebin
directory (relative to the installation directory):Reference:
ExpandConstant
Note #1: Install step add path only once (ensures repeatability of the installation).
Note #2: Uninstall step remove only one occurrence of the path from variable.
Bonus: Installation step with checkbox "Add to PATH variable".
To add installation step with checkbox "Add to PATH variable" define new task in
[Tasks]
section (checked by default):Then you can check it in
CurStepChanged
event:您可以在 InnoSetup 脚本文件中使用 LegRoom.net 的 modpath.iss 脚本:
You can use LegRoom.net's modpath.iss script in your InnoSetup script file:
@mghie 的答案中的
NeedsAddPath
不会检查尾随\ 和字母大小写。修复它。
The
NeedsAddPath
in the answer by @mghie doesn't check trailing\
and letter case. Fix it.我要感谢大家对这个问题的贡献。我已将 Wojciech Mleczek 发布的大约 95% 的代码合并到我的应用程序的安装程序中。我确实对该代码进行了一些更正,可能对其他人有用。我的更改:
将形式参数
Path
重命名为instlPath
。减少代码中“Path”的多次使用(更易于阅读,IMO)。安装/卸载时,添加对以
\;
结尾的instlPath
的存在性检查。安装/卸载时,添加对以
\;
结尾的instlPath
的存在性检查安装过程中,不要在当前
%PATH%
中重复;
。在安装过程中处理丢失或空的
%PATH%
。在卸载期间,确保起始索引 0 不会传递给
Delete()
。这是我的
EnvAddPath()
的更新版本:以及
EnvRemovePath()
的更新版本:I want to thank everyone for their contributions to this question. I've incorporated about 95% of the code posted by Wojciech Mleczek into my app's installer. I do have some corrections to that code that may prove useful to others. My changes:
Renamed formal argument
Path
toinstlPath
. Cuts down on multiple uses of "Path" in code (easier to read, IMO).When installing/uninstalling, add an existence check for an
instlPath
that ends with\;
.During installation, don't double up
;
in the current%PATH%
.Handle missing or empty
%PATH%
during installation.During uninstall, make sure that a starting index of 0 is not passed to
Delete()
.Here's my updated version of
EnvAddPath()
:And an updated version of
EnvRemovePath()
:这是解决该问题的完整解决方案,它忽略大小写,检查是否存在以
\
结尾的路径,并扩展参数中的常量:Here is a complete solution to the problem that ignores casing, checks for existence of path ending with
\
and also expands the constants in the param:如果您同意使用外部 DLL,PathMgr.dll 也可能是一个选项。
有一个 示例 .iss 脚本演示了如何使用Inno Setup 6 或更高版本中的 DLL。
PathMgr.dll 受 LPGL 许可证保护。
If you are ok with using an external DLL, PathMgr.dll could also be an option.
There is a sample .iss script that demonstrates how to use the DLL in Inno Setup 6 or later.
PathMgr.dll is covered by the LPGL license.
我刚刚添加了这个并且它起作用了:
I just added this and it worked: