72

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?

8 Answers 8

97

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:

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"

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:

[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.

7
  • 3
    Another thing is that the path might be there but use a different character case (easily fixed by using UpperCase or somesuch function) or, even worse, use 8.3 path names (e.g. "C:\Progra~1\MyProg") or environment variables (e.g. "%programfiles%\MyProg"). It'd be a nightmare to detect those as well... Mar 4, 2011 at 13:03
  • 3
    I have to say the way you used Pos() was rather ingenious. I would have split the string by semi-colons into an array, and looped through each one. I don't think I would have thought of this approach. Dec 7, 2011 at 15:48
  • 6
    Set ChangesEnvironment=yes in [Setup] and you can remove the requirement to restart with this. Source May 23, 2012 at 21:14
  • 2
    NeedsAddPath doesn't seems to work. It always add path. Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{drive:c:\}\{#WwwRoot}\php\php5.5"; Check: NeedsAddPath('{drive:c:\}\{#WwwRoot}\php\php5.5'); Flags: preservestringtype;
    – vee
    Nov 5, 2014 at 15:30
  • 3
    @vee: Have you tried the ExpandConstant() function on your parameter?
    – mghie
    Nov 5, 2014 at 20:35
34

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:

[Code]
const EnvironmentKey = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment';

procedure EnvAddPath(Path: string);
var
    Paths: string;
begin
    { Retrieve current path (use empty string if entry not exists) }
    if not RegQueryStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths)
    then Paths := '';

    { Skip if string already found in path }
    if Pos(';' + Uppercase(Path) + ';', ';' + Uppercase(Paths) + ';') > 0 then exit;

    { App string to the end of the path variable }
    Paths := Paths + ';'+ Path +';'

    { Overwrite (or create if missing) path environment variable }
    if RegWriteStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths)
    then Log(Format('The [%s] added to PATH: [%s]', [Path, Paths]))
    else Log(Format('Error while adding the [%s] to PATH: [%s]', [Path, Paths]));
end;

procedure EnvRemovePath(Path: string);
var
    Paths: string;
    P: Integer;
begin
    { Skip if registry entry not exists }
    if not RegQueryStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths) then
        exit;

    { Skip if string not found in path }
    P := Pos(';' + Uppercase(Path) + ';', ';' + Uppercase(Paths) + ';');
    if P = 0 then exit;

    { Update path variable }
    Delete(Paths, P - 1, Length(Path) + 1);

    { Overwrite path environment variable }
    if RegWriteStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths)
    then Log(Format('The [%s] removed from PATH: [%s]', [Path, Paths]))
    else Log(Format('Error while removing the [%s] from PATH: [%s]', [Path, Paths]));
end;

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 and CurUninstallStepChanged to remove it when user uninstall an application. In below example script add/remove the bin directory (relative to the installation directory):

#include "environment.iss"

[Setup]
ChangesEnvironment=true

; More options in setup section as well as other sections like Files, Components, Tasks...

[Code]
procedure CurStepChanged(CurStep: TSetupStep);
begin
    if CurStep = ssPostInstall 
     then EnvAddPath(ExpandConstant('{app}') +'\bin');
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
    if CurUninstallStep = usPostUninstall
    then EnvRemovePath(ExpandConstant('{app}') +'\bin');
end;

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".

Inno Setup - Add to PATH variable

To add installation step with checkbox "Add to PATH variable" define new task in [Tasks] section (checked by default):

[Tasks]
Name: envPath; Description: "Add to PATH variable" 

Then you can check it in CurStepChanged event:

procedure CurStepChanged(CurStep: TSetupStep);
begin
    if (CurStep = ssPostInstall) and IsTaskSelected('envPath')
    then EnvAddPath(ExpandConstant('{app}') +'\bin');
end;
8
  • @wojciech Thank you. What to do if I have to add two folders to PATH?
    – Suman
    Aug 17, 2019 at 3:51
  • @SumanKhanal Call EnvAddPath and EnvRemovePath 2 times inside CurStepChanged and CurUninstallStepChanged functions. Aug 24, 2019 at 8:46
  • Best answer here, works well for installing and uninstalling cleanly. Mar 17, 2020 at 20:50
  • @WojciechMleczek Thanks! This one is the best method I have tried for adding a folder to PATH!
    – Lumito
    Jul 22, 2020 at 17:23
  • Windows 10 64-bit - it updates the registry entry but it doesn't show up in 'path' command output on command line. I'm not sure what's going wrong but I'm not going to spend a bunch of time trying to figure it out right now. Just wanted to mention.
    – Walt
    Oct 25, 2022 at 18:41
18

You can use LegRoom.net's modpath.iss script in your InnoSetup script file:

#define MyTitleName "MyApp" 

[Setup]
ChangesEnvironment=yes

[CustomMessages]
AppAddPath=Add application directory to your environmental path (required)

[Files]
Source: "install\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs; 

[Icons]
Name: "{group}\{cm:UninstallProgram,{#MyTitleName}}"; Filename: "{uninstallexe}"; Comment: "Uninstalls {#MyTitleName}"
Name: "{group}\{#MyTitleName}"; Filename: "{app}\{#MyTitleName}.EXE"; WorkingDir: "{app}"; AppUserModelID: "{#MyTitleName}"; Comment: "Runs {#MyTitleName}"
Name: "{commondesktop}\{#MyTitleName}"; Filename: "{app}\{#MyTitleName}.EXE"; WorkingDir: "{app}"; AppUserModelID: "{#MyTitleName}"; Comment: "Runs {#MyTitleName}"

[Registry]
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{app}"

[Tasks]
Name: modifypath; Description:{cm:AppAddPath};   

[Code]

const
    ModPathName = 'modifypath';
    ModPathType = 'system';

function ModPathDir(): TArrayOfString;
begin
    setArrayLength(Result, 1)
    Result[0] := ExpandConstant('{app}');
end;

#include "modpath.iss"
2
  • Thanks works like a charm. By removing some pieces of code in modpath.iss it is also possible to make it run without asking the user (i.e. not as a Task with checkbox but always). Sep 27, 2018 at 17:53
  • @JohannesSchaub-litb Wow, this is heavy. Since the original link is now marked as dangerous (I don't know why), but a simple archive lookup noted the script as GPL3 license web.archive.org/web/20170610232441/https://www.legroom.net/…
    – ecle
    Jan 13, 2020 at 2:16
8

The NeedsAddPath in the answer by @mghie doesn't check trailing \ and letter case. Fix it.

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(';' + UpperCase(Param) + ';', ';' + UpperCase(OrigPath) + ';') = 0) and
    (Pos(';' + UpperCase(Param) + '\;', ';' + UpperCase(OrigPath) + ';') = 0); 
end;
3
  • 4
    How do I use a variable instead of 'C:\foo'? I tried NeedsAddPath('{app}') but it does not work - just concatenating the path although it's already exit. Can you advice please? Sep 21, 2012 at 9:46
  • 1
    Just to answer above comment, might be useful to others: You need to use ExpandConstant() function.
    – Jack
    Aug 1, 2014 at 15:36
  • Thank you Jack. However I would love to see an example of NeedsAddPath('{app}\MoreDirectoriesHere')
    – vezenkov
    Jul 30, 2015 at 16:03
6

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 to instlPath. 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():

const EnvironmentKey = 'Environment';

procedure EnvAddPath(instlPath: string);
var
    Paths: string;
begin
    { Retrieve current path (use empty string if entry not exists) }
    if not RegQueryStringValue(HKEY_CURRENT_USER, EnvironmentKey, 'Path', Paths) then
        Paths := '';

    if Paths = '' then
        Paths := instlPath + ';'
    else
    begin
        { Skip if string already found in path }
        if Pos(';' + Uppercase(instlPath) + ';',  ';' + Uppercase(Paths) + ';') > 0 then exit;
        if Pos(';' + Uppercase(instlPath) + '\;', ';' + Uppercase(Paths) + ';') > 0 then exit;

        { Append App Install Path to the end of the path variable }
        Log(Format('Right(Paths, 1): [%s]', [Paths[length(Paths)]]));
        if Paths[length(Paths)] = ';' then
            Paths := Paths + instlPath + ';'  { don't double up ';' in env(PATH) }
        else
            Paths := Paths + ';' + instlPath + ';' ;
    end;

    { Overwrite (or create if missing) path environment variable }
    if RegWriteStringValue(HKEY_CURRENT_USER, EnvironmentKey, 'Path', Paths)
    then Log(Format('The [%s] added to PATH: [%s]', [instlPath, Paths]))
    else Log(Format('Error while adding the [%s] to PATH: [%s]', [instlPath, Paths]));
end;

And an updated version of EnvRemovePath():

procedure EnvRemovePath(instlPath: string);
var
    Paths: string;
    P, Offset, DelimLen: Integer;
begin
    { Skip if registry entry not exists }
    if not RegQueryStringValue(HKEY_CURRENT_USER, EnvironmentKey, 'Path', Paths) then
        exit;

    { Skip if string not found in path }
    DelimLen := 1;     { Length(';') }
    P := Pos(';' + Uppercase(instlPath) + ';', ';' + Uppercase(Paths) + ';');
    if P = 0 then
    begin
        { perhaps instlPath lives in Paths, but terminated by '\;' }
        DelimLen := 2; { Length('\;') }
        P := Pos(';' + Uppercase(instlPath) + '\;', ';' + Uppercase(Paths) + ';');
        if P = 0 then exit;
    end;

    { Decide where to start string subset in Delete() operation. }
    if P = 1 then
        Offset := 0
    else
        Offset := 1;
    { Update path variable }
    Delete(Paths, P - Offset, Length(instlPath) + DelimLen);

    { Overwrite path environment variable }
    if RegWriteStringValue(HKEY_CURRENT_USER, EnvironmentKey, 'Path', Paths)
    then Log(Format('The [%s] removed from PATH: [%s]', [instlPath, Paths]))
    else Log(Format('Error while removing the [%s] from PATH: [%s]', [instlPath, Paths]));
end;
0
2

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:

function NeedsAddPath(Param: string): boolean;
var
  OrigPath: string;
  ParamExpanded: string;
begin
  //expand the setup constants like {app} from Param
  ParamExpanded := ExpandConstant(Param);
  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 and with or without \ ending
  // Pos() returns 0 if not found
  Result := Pos(';' + UpperCase(ParamExpanded) + ';', ';' + UpperCase(OrigPath) + ';') = 0;  
  if Result = True then
     Result := Pos(';' + UpperCase(ParamExpanded) + '\;', ';' + UpperCase(OrigPath) + ';') = 0; 
end;
1
  • Pretty sure there is an issue here: It only checks the '\;' case only if the ; case was found.
    – Stewart
    Aug 9, 2017 at 14:48
0

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.

0

I just added this and it worked:

[Setup]
ChangesEnvironment=yes

[Registry]
Root: HKCU; Subkey: "Environment"; ValueType:string; ValueName: "Path"; ValueData: "{olddata};{app}";
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Aug 4, 2023 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.