procedure EnvAddPath(installPath: string);
var
Paths: string;
begin
{ 检索当前的路径 }
if not RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', Paths) then
Paths := '';
if Paths = '' then
Paths := installPath + ';'
else
begin
{ 如果已在路径中找到字符串则跳过 }
if Pos(';' + Uppercase(installPath) + ';', ';' + Uppercase(Paths) + ';') > 0 then exit;
if Pos(';' + Uppercase(installPath) + '\\;', ';' + Uppercase(Paths) + ';') > 0 then exit;
{ 将应用安装路径附加到路径变量的末尾 }
Log(Format('Right(Paths, 1): [%s]', [Paths[length(Paths)]]));
if Paths[length(Paths)] = ';' then
Paths := Paths + installPath + ';' { 不要加倍';'在环境(路径)中 }
else
Paths := Paths + ';' + installPath + ';' ;
end;
{ 覆盖(或在缺少时创建)路径环境变量 }
if RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', Paths)
then Log(Format('The [%s] added to PATH: [%s]', [installPath, Paths]))
else Log(Format('Error while adding the [%s] to PATH: [%s]', [installPath, Paths]));
end;
procedure EnvRemovePath(instlPath: string);
var
Paths: string;
P, Offset, DelimLen: Integer;
begin
{ 如果注册表项不存在则跳过 }
if not RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', Paths) then
exit;
{ 如果在路径中找不到字符串则跳过 }
DelimLen := 1; { Length(';') }
P := Pos(';' + Uppercase(instlPath) + ';', ';' + Uppercase(Paths) + ';');
if P = 0 then
begin
{ 也许 instlPath 存在于 Paths 中,但以 '\\;' 结尾 }
DelimLen := 2; { Length('\\;') }
P := Pos(';' + Uppercase(instlPath) + '\\;', ';' + Uppercase(Paths) + ';');
if P = 0 then exit;
end;
{ 确定在 Delete() 操作中从何处开始字符串子集。 }
if P = 1 then
Offset := 0
else
Offset := 1;
{ 更新路径变量 }
Delete(Paths, P - Offset, Length(instlPath) + DelimLen);
{ 覆盖路径环境变量 }
if RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', '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;
procedure CurStepChanged(CurStep: TSetupStep); begin if CurStep = ssPostInstall then EnvAddPath(ExpandConstant('{app}')); end; procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep); begin if CurUninstallStep = usPostUninstall then EnvRemovePath(ExpandConstant('{app}')); end;