Inno Setup - 卸载程序时从PATH环境变量中删除路径

Mil*_*uca 3 windows installer inno-setup environment-variables

我写了一个Inno Setup脚本,它安装程序并用安装程序PATH的目录更新 环境变量.

我想更新PATH环境变量,以恢复其先前的安装状态.

安装程序运行时,用户会选择安装路径.

这是脚本,它使用如何在运行Inno Setup Installer时修改PATH环境变量中的代码

[Setup]
AppName=Pandoc_x64
AppVersion=1.16.0.2
AppPublisher=Hitachi Systems CBT
DefaultDirName={pf64}\pandoc
UninstallDisplayName=Pandoc_x64
DisableDirPage=no
UninstallFilesDir={app}\uninstall

[Files]
Source: "pandoc.exe"; DestDir: "{app}";
Source: "pandoc-citeproc.exe"; DestDir: "{app}";


[Setup]
ChangesEnvironment=yes

[Registry]
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; \
    ValueType: expandsz; ValueName: "PATH"; ValueData: "{olddata};{app}"; \
    Check: NeedsAddPath('{app}')
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment";  ValueName: "PATH"; ValueData: "{app}"; Flags: uninsdeletevalue

[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;
Run Code Online (Sandbox Code Playgroud)

看一下代码,可以注意以下说明:

Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment";  ValueName: "PATH"; ValueData: "{app}"; Flags: uninsdeletevalue
Run Code Online (Sandbox Code Playgroud)

我使用了那个指令,改编(在我看来)为我的例子,阅读Inno Setup.如何卸载注册表值?

使用uninsdeletevalue应该是在卸载程序时删除值,事实上,当我运行卸载程序时,整个PATH变量被删除,但我需要将PATH环境变量恢复到以前的安装值.我认为在运行安装程序之前可以读取它的值,但我不知道在卸载阶段如何使用它.

有人可以用代码示例帮我吗?

Mar*_*ryl 7

[Registry]仅使用部分条目卸载时,您无法让Inno Setup记住安装时的值并将其恢复.

虽然您可以对其进行编码,但无论如何都不是好的方法,因为PATH安装后可能会发生变化,您将放弃任何此类更改.


您必须搜索PATH路径并仅删除路径.

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

procedure RemovePath(Path: string);
var
  Paths: string;
  P: Integer;
begin
  if not RegQueryStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths) then
  begin
    Log('PATH not found');
  end
    else
  begin
    Log(Format('PATH is [%s]', [Paths]));

    P := Pos(';' + Uppercase(Path) + ';', ';' + Uppercase(Paths) + ';');
    if P = 0 then
    begin
      Log(Format('Path [%s] not found in PATH', [Path]));
    end
      else
    begin
      if P > 1 then P := P - 1;
      Delete(Paths, P, Length(Path) + 1);
      Log(Format('Path [%s] removed from PATH => [%s]', [Path, Paths]));

      if RegWriteStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths) then
      begin
        Log('PATH written');
      end
        else
      begin
        Log('Error writing PATH');
      end;
    end;
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then
  begin
    RemovePath(ExpandConstant('{app}'));
  end;
end;
Run Code Online (Sandbox Code Playgroud)