У меня была та же проблема, но, несмотря на ответы, приведенные выше, я нашел собственное решение и хотел бы поделиться им с вами.
Прежде всего, я создал environment.iss
файл с двумя методами - один для добавления пути к переменной Path * среды, а второй для его удаления:
[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;
Ссылка: RegQueryStringValue
, RegWriteStringValue
Теперь в основном файле .iss я мог бы включить этот файл и прослушать 2 события (подробнее о событиях вы можете узнать в разделе Функции событий в документации),CurStepChanged
для добавления пути после установки и CurUninstallStepChanged
для его удаления при удалении пользователем приложения.В приведенном ниже примере сценария добавьте / удалите каталог bin
(относительно каталога установки):
#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;
Ссылка: ExpandConstant
Примечание# 1 : шаг установки добавить путь только один раз (обеспечивает повторяемость установки).
Примечание # 2 : шаг удаления удаляет только одно вхождение пути из переменной.
Бонус : шаг установки с флажком «Добавить в переменную PATH» .
![Inno Setup - Add to PATH variable](https://i.stack.imgur.com/joGDh.png)
Чтобы добавить шаг установки с флажком «Добавить в переменную PATH» определить новое задание в разделе [Tasks]
(по умолчанию установлено):
[Tasks]
Name: envPath; Description: "Add to PATH variable"
Затем вы можете проверить его в CurStepChanged
событие:
procedure CurStepChanged(CurStep: TSetupStep);
begin
if (CurStep = ssPostInstall) and IsTaskSelected('envPath')
then EnvAddPath(ExpandConstant('{app}') +'\bin');
end;