Inno Setup - 卸载程序时从PATH环境变量中删除路径 [英] Inno Setup - Remove path from PATH environment variable while uninstalling a program
问题描述
我写了一个Inno Setup脚本,它安装一个程序,并使用安装程序的目录更新
PATH
环境变量。
I wrote an Inno Setup script which install a program and update the
PATH
environment variable with the directory in which the program in installed.
我想更新 PATH
环境变量,以恢复其以前的安装状态。
I want to update the PATH
environment variable, to restore its previous installation status.
安装程序运行时,用户选择安装路径。
The installation path is chosen by the user while the installer is running.
这是脚本,使用如何在运行Inno安装程序安装程序时修改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;
查看代码,可以注意以下说明:
Taking a look to the code, it's possible to note the following instruction:
Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueName: "PATH"; ValueData: "{app}"; Flags: uninsdeletevalue
我用这个指令,在我看来,适用于我的例子,一个href =https://stackoverflow.com/q/26762668/850848> Inno安装程序。如何卸载注册表值?
I used that instruction, adapted (in my opinion) for the my example, reading Inno Setup. How to uninstall registry value?
使用 uninsdeletevalue
应该在程序中删除该值被卸载,事实上,当我运行卸载程序时,整个 PATH
变量被删除,但是我需要恢复 PATH
环境变量为先前的安装值。
我认为在运行安装程序之前可能会读取它的价值,但我不知道如何在卸载阶段使用它。
The use of uninsdeletevalue
should be delete the value when the program is uninstalled, and in fact, when I run the uninstaller, the entire PATH
variable is deleted, but I need to restore the PATH
environment variable to the previous installation value.
I think it's possible reading its value before run the installer, but I don't have any idea to how use it in the uninstall phase.
有人可以帮助我一个代码示例?
Can someone help me with a code example?
推荐答案
您不能让Inno Setup记住安装时的价值,并使用 [注册表]
部分条目。
You cannot have Inno Setup remember the value on installation and restore it, when uninstalling using [Registry]
section entry only.
虽然你可以编写代码,但这并不好,因为 PATH
可能在安装后发生更改,您将丢弃任何此类更改。
While you can code it, it's not good approach anyway as the PATH
likely changes after the installation and you will discard any such changes.
您必须搜索 PATH
为您的路径,只删除路径。
You have to search the PATH
for your path and remove the path only.
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
Delete(Paths, P - 1, 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;
这篇关于Inno Setup - 卸载程序时从PATH环境变量中删除路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!