Delete Self

eNarc
eNarc used Ask the Experts™
on
Hi, I'm making an uninstaller.

I've been able to delete everything though only thing I'm unable to delete is the exe itself..

is there away of deleting the exe?

could I open up command to delete the file?? or anything like that?

procedure RegisterUninstall(Name, Path: String);
var
  AReg: TRegistry;
begin
  AReg:= TRegistry.Create;
  AReg.RootKey:= HKEY_LOCAL_MACHINE;
  AReg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Uninstall', false);
  AReg.OpenKey(Name, true);
  AReg.WriteString('DisplayName', Name);
  AReg.WriteString('UninstallString', Path);
  AReg.Free;
end;

using the -uninstall paramstr

though the item in add and remove still stays there and the exe stays there also

anyway of deleting the exe and the entry in add and remove programs when removing.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
try this

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);
       ShellExecute(Form1.Handle, nil, PChar(batchname), nil, nil, SW_HIDE);
     finally
       batchfile.Free;
     end;
     Halt;
   end;
 end;

Open in new window

Commented:
To delete not empty folder please check line

       Add('rmdir "' + ExtractFilePath(ParamStr(0)) + '"');

Open in new window


to

       Add('rmdir "' + ExtractFilePath(ParamStr(0)) + '" /s /q');

Open in new window

Top Expert 2011
Commented:
You can use batch file as explained in this link.

Or you may check this link for self deleting.

Here is a demo for handling the Control Panel - Add/Remove Programs.
Commented:
or you can simply call this to delete after reboot:

MoveFileEx(PChar(ParamStr(0)), nil, MOVEFILE_DELAY_UNTIL_REBOOT)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial