Inno Setup - 卸载程序时从 PATH 环境变量中删除路径 [英] Inno Setup - Remove path from PATH environment variable while uninstalling a program
问题描述
我写了一个 Inno Setup 脚本来安装一个程序并更新PATH
环境变量与安装程序的目录.
我想更新 PATH
环境变量,以恢复其之前的安装状态.
安装路径由用户在安装程序运行时选择.
这是脚本,它使用来自 如何在运行 Inno Setup Installer 时修改 PATH 环境变量?
[设置]更改环境=是[注册表]根:HKLM;子项:SYSTEMCurrentControlSetControlSession ManagerEnvironment";值类型:expandsz;值名称:路径";值数据:{olddata};{app}";检查:NeedsAddPath('{app}')根:HKLM;子项:SYSTEMCurrentControlSetControlSession ManagerEnvironment";值名称:路径";值数据:{app}";标志:uninsdeletevalue
[代码]函数NeedsAddPath(参数:字符串):布尔值;变量原始路径:字符串;开始如果不是 RegQueryStringValue(HKEY_LOCAL_MACHINE,'SYSTEMCurrentControlSetControlSession ManagerEnvironment','路径',原始路径)然后开始结果:=真;出口;结尾;{ 查找带有分号和结尾分号的路径 }{ Pos() 如果没有找到则返回 0 }结果 := Pos(';' + Param + ';', ';' + OrigPath + ';') = 0;结尾;
查看代码,可以注意到以下指令:
根:HKLM;子项:SYSTEMCurrentControlSetControlSession ManagerEnvironment";值名称:路径";值数据:{app}";标志:uninsdeletevalue
我使用了该指令,适应了(在我看来)我的示例,阅读 Inno Setup.如何卸载注册表值?
uninsdeletevalue
的使用应该是程序卸载时删除的值,而实际上我运行卸载程序的时候,整个PATH
变量都被删除了,但是我需要将 PATH
环境变量恢复为之前的安装值.我认为可以在运行安装程序之前读取它的值,但我不知道如何在卸载阶段使用它.
有人可以帮我提供一个代码示例吗?
您无法让 Inno Setup 记住安装时的值并在仅使用 [Registry]
部分条目进行卸载时恢复它.p>
虽然您可以对其进行编码,但这并不是一个好方法,因为 PATH
可能会在安装后发生更改,您将放弃任何此类更改.
您必须在 PATH
中搜索您的路径并仅删除该路径.
constEnvironmentKey = 'SYSTEMCurrentControlSetControlSession ManagerEnvironment';过程 RemovePath(路径:字符串);变量路径:字符串;P:整数;开始如果不是 RegQueryStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths) 那么开始Log('找不到路径');结尾别的开始Log(Format('PATH is [%s]', [Paths]));P := Pos(';' + 大写(路径) + ';', ';' + 大写(路径) + ';');如果 P = 0 那么开始Log(Format('Path [%s] not found in PATH', [Path]));结尾别的开始如果 P >1 然后 P := P - 1;删除(路径,P,长度(路径)+ 1);Log(Format('Path [%s] 从 PATH 中移除 => [%s]', [Path, Paths]));如果 RegWriteStringValue(HKEY_LOCAL_MACHINE, EnvironmentKey, 'Path', Paths) 那么开始Log('路径写入');结尾别的开始Log('写入路径错误');结尾;结尾;结尾;结尾;过程 CurUninstallStepChanged(CurUninstallStep: TUninstallStep);开始如果 CurUninstallStep = usUninstall 那么开始RemovePath(ExpandConstant('{app}'));结尾;结尾;
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.
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.
This is the script, which uses code from How do I modify the PATH environment variable when running an Inno Setup Installer?
[Setup]
ChangesEnvironment=yes
[Registry]
Root: HKLM; Subkey: "SYSTEMCurrentControlSetControlSession ManagerEnvironment";
ValueType: expandsz; ValueName: "PATH"; ValueData: "{olddata};{app}";
Check: NeedsAddPath('{app}')
Root: HKLM; Subkey: "SYSTEMCurrentControlSetControlSession ManagerEnvironment";
ValueName: "PATH"; ValueData: "{app}"; Flags: uninsdeletevalue
[Code]
function NeedsAddPath(Param: string): boolean;
var
OrigPath: string;
begin
if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
'SYSTEMCurrentControlSetControlSession ManagerEnvironment',
'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: "SYSTEMCurrentControlSetControlSession ManagerEnvironment";
ValueName: "PATH"; ValueData: "{app}"; Flags: uninsdeletevalue
I used that instruction, adapted (in my opinion) for the my example, reading Inno Setup. How to uninstall registry value?
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?
You cannot have Inno Setup remember the value on installation and restore it, when uninstalling using [Registry]
section entry only.
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.
You have to search the PATH
for your path and remove the path only.
const
EnvironmentKey = 'SYSTEMCurrentControlSetControlSession ManagerEnvironment';
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;
这篇关于Inno Setup - 卸载程序时从 PATH 环境变量中删除路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!