Delphi - improve my code to self delete executable

460 Views Asked by At

I always worked with this code to self-delete my executable. But I think that's a ridicule way to do it. What can I do to improve this? I guess using a external Batch isn't even correct. Code Below:

  procedure SelfDelete;
   var
   myFile : TextFile;
   begin
     AssignFile(myFile, 'C:\Test.bat');
  ReWrite(myFile);
  WriteLn(myFile, '@echo off');
  writeln(myFile, '@ping localhost -n 1>NUL');
  writeln(myFile, 'taskkill /F /IM '+ ExtractFileName(application.exename));
  writeln(myFIle, 'del /s /q "'+Application.Exename+'"');
    writeln(myFile, 'del /s /q "%~f0"');
  CloseFile(myFile);
   end;

and OnClose Event I did.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
WinExec('C:\Test.bat',0);
end;

Somebody can help made it to show less moron? Thanks

0

There are 0 best solutions below