1

I'm using Inno Setup 5.5.6 (I can install a newer version if required) and I want to build a setup which deletes itself after it has been run.

My current approach (which doesn't work) is:

[Files]    
Source: files\MyFile.dat DestDir: {app}; Flags: ignoreversion; \
    AfterInstall: DeleteUpdateIfRequested;

and

function CmdLineParamExists(const Value: string): Boolean;
var
  I: Integer;  
begin
  Result := False;
  for I := 1 to ParamCount do
    if CompareText(ParamStr(I), Value) = 0 then
    begin
      Result := True;
      Exit;
    end;
end;

procedure DeleteUpdateIfRequested;
var
  DeleteResult : Boolean;
begin
  if CmdLineParamExists('/DELETESETUP') then
  begin
    MsgBox('Deleting file ' + ExpandConstant('{srcexe}'), mbInformation, MB_OK);
    DeleteResult := DeleteFile(ExpandConstant('{srcexe}'));

    if DeleteResult then
      MsgBox('deleted', mbInformation, MB_OK)
    else
      MsgBox('not deleted', mbInformation, MB_OK);
  end
  else
    MsgBox('Parameter nicht erkannt', mbInformation, MB_OK);
end;

The "Deleting..." message appears and the "not deleted..." one. It might be, that the setup.exe is still locked as it is still running. Is there maybe a later "step" where I could run the code?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Markus
  • 129
  • 1
  • 12

1 Answers1

1

First, I have to say that I do not think this all is a good idea, anyway...

The installer is locked as long as it is running. So no code in the installer process itself can delete it.

What you can do is to run another process that will delete the installer, once the installer closes.

Simple cmd process with delayed (and possibly retrying) del command would do. For such solution see a similar question:
Unload a .NET DLL from an unmanaged process


Another similar question about uninstaller has been asked recently:
How to delete iu-....tmp folder which is created during Inno Setup uninstallation?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992