Backup files and restore them on uninstall with In

2019-01-24 18:48发布

问题:

Consider the following:

  • I have two files, for example XXX.txt and YYY.txt

  • I want to install them to a folder (let's say files), in which there are already XXX.txt and YYY.txt files

  • I want to "back up" the two original files, renaming them to XXX.txt.backup and YYY.txt.backup

  • On uninstall I want to restore the two files to their original state

How can I achieve this with Inno Setup?

回答1:

Add

[Files]
; Backup Function_Template
Source: "{app}\XXX.txt"; DestDir: "{app}"; DestName: "XXX.txt.bkup"; Flags: external skipifsourcedoesntexist uninsneveruninstall

That would move the existing file, and the flags will prevent from uninstalling it. Now in the code you can put

[Code] 
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  OldFile: string;
begin
  case CurUninstallStep of    
    usPostUninstall:
      begin
        OldFile := ExpandConstant('{app}\XXX.txt.bkup');
        if FileExists(OldFile) then
          RenameFile(OldFile, ExpandConstant('{app}\XXX.txt'));
      end;
  end;
end;


回答2:

Source: "{app}\XXX.txt"; DestDir: "{app}"; DestName: "XXX.txt.bkup"; Flags: external skipifsourcedoesntexist uninsneveruninstall

Did not appear to work, since "The compiler will prepend the path of your installation's source directory if you do not specify a fully qualified pathname."

However, I just discovered this works fine! I had left off the "external" flag.



回答3:

Well, maybe a popup saying "There is already an XXX.txt.backup. Do you really want to overwrite it?"



标签: inno-setup