我使用并行安装软件,其中每个新版本都安装到单独的文件夹中,例如“MySoftware2”、“MySoftware3”等。这不是一个好主意,我想移动到单个文件夹文件夹安装,其中所有安装和更新都将放入
"MySoftware"
文件夹(没有版本号)。
对于所有全新安装,该文件夹将为
DefaultDirName={pf}\MySoftware
但是软件以前版本的自动更新(例如从 v3 到 v4)又如何呢?我想这些应该安装到现有文件夹(“MySoftware3”)中,然后重命名? Inno如何知道现有文件夹以及安装后Inno能否重命名安装目录?
知道如何正确执行此操作吗?谢谢。
RenameFile
中的CurStepChanged(ssPostInstall)
功能。这是最简单的部分。
但您还需要更新卸载注册表信息。这是更多的工作。
这样的事情就可以了。
#define AppId "My Program"
[Setup]
AppId={#AppId}
[Code]
const
OldDirName = 'MySoftware3';
NewDirName = 'MySoftware';
InnoSetupReg =
'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#AppId}_is1';
var
Path, NewPath: string;
RootKey: Integer;
function CheckRegistry(
ValueName: string; var P: Integer; var Value: string): Boolean;
begin
if not RegQueryStringValue(RootKey, InnoSetupReg, ValueName, Value) then
begin
Log(Format('Cannot find uninstall registry value "%s" in key %s', [
ValueName, InnoSetupReg]));
Result := False;
end
else
begin
P := Pos(Lowercase(Path), Lowercase(Value));
Result := (P > 0);
if not Result then
begin
Log(Format(
'Uninstall registry value "%s" in key %s "%s" ' +
'does not contain the installation path "%s"', [
ValueName, InnoSetupReg, Value, Path]));
end;
end
end;
procedure UpdateRegistry(ValueName: string);
var
Value, NewValue: string;
P: Integer;
begin
if CheckRegistry(ValueName, P, Value) then
begin
NewValue :=
Copy(Value, 1, P - 1) +
NewPath +
Copy(Value, P + Length(Path), Length(Value) - P - Length(Path) + 1);
if not RegWriteStringValue(RootKey, InnoSetupReg, ValueName, NewValue) then
begin
Log(Format('Cannot write uninstall registry value "%s" in key %s', [
ValueName, InnoSetupReg]));
end
else
begin
Log(Format('Updated uninstall registry value "%s" from "%s" to "%s"', [
ValueName, Value, NewValue]));
end;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
Value: string;
P: Integer;
begin
Log(Format('CurStepChanged %d', [CurStep]));
if CurStep = ssPostInstall then
begin
Path := ExpandConstant('{app}');
if CompareText(ExtractFileName(Path), OldDirName) = 0 then
begin
Log(Format(
'Program was installed to old folder name "%s", trying to rename...', [
Path]));
NewPath := ExtractFilePath(Path) + NewDirName;
if IsAdminInstallMode then RootKey := HKLM
else RootKey := HKCU;
if CheckRegistry('UninstallString', P, Value) then
begin
if not RenameFile(Path, NewPath) then
begin
Log('Failed to rename');
end
else
begin
Log('Renamed, updating uninstall registry information...');
UpdateRegistry('UninstallString');
UpdateRegistry('QuietUninstallString');
UpdateRegistry('DisplayIcon');
UpdateRegistry('Inno Setup: App Path');
UpdateRegistry('InstallLocation');
Log('Installation path rename done');
end;
end;
end;
end;
end;
但请注意,它依赖旧名称而不是新名称的一部分,就像您现在拥有的那样(
MySoftware
=> MySoftware3
)。否则每次更新都会不断重命名。