Inno Setup:在安装删除部分之前备份外部文件

Inno Setup: Backup external file before install delete section

我会在 [InstallDelete] 部分删除文件和文件夹之前对其进行备份

[Files]
Source: "{app}\res_mods\configs\wotstat\cache.json"; \
  DestDir: "{app}\_backup\res_mods_{#DateTime}\configs\wotstat\"; \
  Flags: external skipifsourcedoesntexist uninsneveruninstall 
Source: "{app}\res_mods[=10=].9.17.1\vehicles\*"; \
  DestDir:"{app}\_backup\res_mods_{#DateTime}[=10=].9.17.1\vehicles\"; \
  Flags: external skipifsourcedoesntexist createallsubdirs recursesubdirs uninsneveruninstall

这很好用。但是,如果我检查

[InstallDelete]
Type: filesandordirs; Name: "{app}\mods\*.*"; Tasks: cleanres
Type: filesandordirs; Name: "{app}\res_mods\*.*"; Tasks: cleanres

没有文件被保存

我如何让它发挥作用。谢谢

[InstallDelete] 部分在 [Files] 部分之前处理(正如人们所期望的那样)。见 installation order.

您可以在 CurStepChanged(ssInstall) event 中对备份进行编码,这发生在安装开始之前:

[Code]

procedure CurStepChanged(CurStep: TSetupStep);
var
  SourcePath: string;
  DestPath: string;
begin
  if CurStep = ssInstall then
  begin
    SourcePath := ExpandConstant('{app}\res_mods[=10=].9.17.1\vehicles');
    DestPath := ExpandConstant('{app}\_backup\res_mods_{#DateTime}[=10=].9.17.1\vehicles');
    Log(Format('Backing up %s to %s before installation', [SourcePath, DestPath]));
    if not ForceDirectories(DestPath) then
    begin
      Log(Format('Failed to create %s', [DestPath]));
    end
      else
    begin
      DirectoryCopy(SourcePath, DestPath);
    end;

    SourcePath := ExpandConstant('{app}\res_mods\configs\wotstat\cache.json');
    DestPath := ExpandConstant('{app}\_backup\res_mods_{#DateTime}\configs\wotstat');
    if not ForceDirectories(DestPath) then
    begin
      Log(Format('Failed to create %s', [DestPath]));
    end
      else
    begin
      if not FileCopy(SourcePath, DestPath + '\cache.json', False) then
      begin
        Log(Format('Failed to copy %s', [SourcePath]));
      end
        else 
      begin
        Log(Format('Backed up %s', [SourcePath]));
      end;
    end;
  end;
end;

该代码使用 中的 DirectoryCopy 函数。