Mega Code Archive

 
Categories / Delphi / System
 

Delete myself

Title: Delete myself Question: How delete a application while it is running Answer: Now here is a standard function as following procedure DeleteMe(const FileName: string); var BatchFile: TextFile; BatchFileName: string; ProcessInfo: TProcessInformation; StartUpInfo: TStartupInfo; begin BatchFileName := ChangeFileExt(FileName, '.BAT'); AssignFile(BatchFile, BatchFileName); Rewrite(BatchFile); Writeln(BatchFile, ':TRY'); Writeln(BatchFile, 'DEL "' + FileName + '"'); Writeln(BatchFile, 'IF EXIST "' + FileName + '"' + ' GOTO TRY'); Writeln(BatchFile, 'DEL %0'); 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;