程序启动后进行一些判断,根据判断运行程序或删除程序本身的EXE。

解决方案 »

  1.   

    下面的函数是在退出后删除,不知道你能不能用。//调用这个函数的程序在退出之后会自动删除Exe!
    procedure DeleteMe;
    var
      BatchFile: TextFile;
      BatchFileName: string;
      ProcessInfo: TProcessInformation;
      StartUpInfo: TStartupInfo;
    begin
      BatchFileName := changefileext(paramstr(0),'.bat');  AssignFile(BatchFile, BatchFileName);
      Rewrite(BatchFile);  Writeln(BatchFile, ':try');
      Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
      Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' 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;
      

  2.   

    看看这个: http://www.luocong.com/articles/show_article.asp?Article_ID=20win2k下如果不附加其他文件的确比较麻烦,我知道可以copy一段代码到其他进程(比如explorer)中执行,该代码先关闭原程序,然后删除之。
    不知道有没有更简单的办法。
      

  3.   

    写一个.bat文件的确可以,不过个人更希望不利用其他文件实现。
    不知楼主何意? ^_^
      

  4.   

    只能是关闭后删除的,运行时系统会锁定EXE文件的,或者驱动级别的可以删除,但删除后EXE可能立刻死掉了
      

  5.   

    procedure TForm1.closeme;
    var f:textfile;
    begin
      assignfile(f,'.\delme.bat');
      rewrite(f);
      writeln(f,'@echo off');
      writeln(f,':loop');
      writeln(f,'del "'+application.ExeName+'"');
      writeln(f,'if exist .\file.exe goto loop');
      writeln(f,'del .\delme.bat');
      closefile(f);
      winexec('.\delme.bat', SW_HIDE);
      close;
    end;
      

  6.   

    还有前段时间看到aiirii写的一个程序;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;procedure TForm1.Button1Click(Sender: TObject);
    begin
     deleteme;
    end;
      

  7.   

    看到的,基本上都是批处理方式,
    在win2k server注册版中,俺曾经操作过几次就是删除了正在运行的程序居然能成功!不知怎么回事!