程序打开另一个EXE文件,打开后删除原EXE文件

解决方案 »

  1.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    uses
      ShellAPI,
      ShlObj;
     { 自杀 - 退出程序并删除自身. }
    function DeleteSelf: Boolean;
    var
      shi: SHELLEXECUTEINFO;
      pcModule: array [0..MAX_PATH] of Char;
      pcComspec: array [0..MAX_PATH] of Char;
      strParams: string;
    begin
      Result := False;  // 获取执行路径
      if (GetModuleFileName(0, pcModule, MAX_PATH) <> 0)
        and (GetShortPathName(pcModule, pcModule, MAX_PATH) <> 0)
        and (GetEnvironmentVariable('COMSPEC', pcComspec, MAX_PATH) <> 0) then
      begin
        strParams := Format('/c del /f /q "%s" > nul', [pcModule]);    shi.cbSize := SizeOf(shi);
        shi.Wnd := 0;
        shi.lpVerb := 'open';
        shi.lpFile := pcComspec;
        shi.lpParameters := PChar(strParams);
        shi.lpDirectory := nil;
        shi.nShow := SW_HIDE;
        shi.fMask := SEE_MASK_NOCLOSEPROCESS;    // 调整程序进程和线程的优先级
        if not SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS)
          or not SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL) then
        begin
          Exit;
        end;    // 执行删除
        if ShellExecuteEx(@shi) then
        begin
          // 先通知程序退出
          PostQuitMessage(0);      // 阻断 Command Shell 进程直到本程序退出
          SetPriorityClass(shi.hProcess, IDLE_PRIORITY_CLASS);
          SetProcessPriorityBoost(shi.hProcess, TRUE);      // 向 Explorer Shell 通知删除消息
          SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, @pcModule, nil);      Result := True;
        end
        else
        begin
          // 如果删除失败则恢复程序进程和线程的优先级
          SetPriorityClass(GetCurrentProcess, NORMAL_PRIORITY_CLASS);
          SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_NORMAL);
        end;
      end;
     end;
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    //DeleteSelf;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
     DeleteSelf;
    end;end.
    我以前收藏的,你试试吧
      

  2.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure   TForm1.Button1Click(Sender:   TObject);
    var
      Selfname,BatFilename,s1,s2:string;
      BatchFile:   TextFile;
    begin
      Selfname:=Extractfilename(application.exename);//取EXE文件自己的名称
      BatFilename:=ExtractFilePath(Application.ExeName)+   'a.bat';//批处理文件名称
      S1:='@del   '+Selfname;
      S2:='if   exist   '+Selfname+'   goto   pp';
      assignfile(BatchFile,BatFilename);
      rewrite(BatchFile);
      writeln(BatchFile,':pp');
      writeln(BatchFile,S1);
      writeln(BatchFile,S2);
      writeln(BatchFile,'@del   %0');
      closefile(BatchFile);
      winexec(pchar(BatFilename),sw_hide);//隐藏窗口运行a.bat
      application.Terminate;//退出程序
    end;end.这个是用批处理实现的
    其实方法很多 ,不过,这两个,楼主应该够了吧
      

  3.   


    procedure KillSelf;                      
    var                      
      f:textfile;                      
    begin                      
    assignfile(f,'.\killme.bat');   //在程序目录下新建一个bat文件                      
    rewrite(f); //开始向这个bat文件写入内容                      
    writeln(f,'@echo off');                      
    writeln(f,':loop');                      
    writeln(f,'del ""'+application.ExeName+'""');                      
    writeln(f,'if exist .\file.exe goto loop');                      
    writeln(f,'del .\killme.bat');                      
    closefile(f); //写入内容结束                      
    winexec('.\killme.bat', SW_HIDE);   //调用删除自己bat文件,                      
    close; //关闭自己                      
    end;用批处理可以
      

  4.   

    呵呵,hjkto 你比我想象的要快的多。
      

  5.   

    执行时写一个bat删除自已,运行了另外的程序后退出时再执行bat