Mega Code Archive

 
Categories / Delphi / Examples
 

How to delete the own program after its finish

Title: How to delete the own program after its finish procedure DeleteEXE; function GetTmpDir: string; var pc: PChar; begin pc := StrAlloc(MAX_PATH + 1); GetTempPath(MAX_PATH, pc); Result := string(pc); StrDispose(pc); end; function GetTmpFileName(ext: string): string; var pc: PChar; begin pc := StrAlloc(MAX_PATH + 1); GetTempFileName(PChar(GetTmpDir), 'uis', 0, pc); Result := string(pc); Result := ChangeFileExt(Result, ext); StrDispose(pc); end; var batchfile: TStringList; batchname: string; begin batchname := GetTmpFileName('.bat'); FileSetAttr(ParamStr(0), 0); batchfile := TStringList.Create; with batchfile do begin try Add(':Label1'); Add('del "' + ParamStr(0) + '"'); Add('if Exist "' + ParamStr(0) + '" goto Label1'); Add('rmdir "' + ExtractFilePath(ParamStr(0)) + '"'); Add('del ' + batchname); SaveToFile(batchname); ChDir(GetTmpDir); ShowMessage('Uninstalling program...'); WinExec(PChar(batchname), SW_HIDE); finally batchfile.Free; end; Halt; end; end;