Mega Code Archive

 
Categories / Delphi / Examples
 

Delete my own Application (beta)

Title: Delete my own Application (beta) Question: How can I remove my Exe-file while my Application is still running? Answer: { Here's the updated version; it seems that the unit doesn't work properly (thx to Guido Geurts). Anyway, it does delete the exe-file as described, but you've to close the App. In the meantime I will try to find a better version - thx for your comprehension. btw, any comments are welcome } uses Windows, SysUtils; procedure DeleteMe; var BatchFile: TextFile; BatchFileName: string; ProcessInfo: TProcessInformation; StartUpInfo: TStartupInfo; begin { create a batchfile in the applications directory } BatchFileName := ExtractFilePath(ParamStr(0)) + '$$336699.bat'; { open and write the file } AssignFile(BatchFile, BatchFileName); Rewrite(BatchFile); Writeln(BatchFile, ':try'); Writeln(BatchFile, 'del "' + ParamStr(0) + '"'); Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try'); Writeln(BatchFile, 'del "' + BatchFileName + '"'); CloseFile(BatchFile); FillChar(StartUpInfo, SizeOf(StartUpInfo), $00); StartUpInfo.dwFlags := STARTF_USESHOWWINDOW; StartUpInfo.wShowWindow := SW_HIDE; if CreateProcess(nil, PChar(BatchFileName), nil, nil, False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then begin CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); end; end;