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?
The
NeedsAddPath
in the answer by @mghie doesn't check trailing\
and letter case. Fix it.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: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.
You can use LegRoom.net's modpath.iss script in your InnoSetup script file:
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: