程序如何把自己完全 删除掉  
也就是说一个EXE文件 运行的时候把自己完全删除掉

解决方案 »

  1.   

    95,winme 可以做到,2000,xp不行.
      

  2.   

    ///////代码开始
    uses
      Windows, Dialogs, SysUtils, Controls;procedure DeleteMe(mDeleteDir: Boolean = False); { 自己删除自己 }
    var
      vExeDir: string;  procedure pDelDir(mDirName: string); { 删除指定路径 }
      var
        vSearchRec: TSearchRec;
        PathName: string;
        K: Integer;
      begin
        PathName := mDirName + '\*.*';
        K := FindFirst(PathName, faAnyFile, vSearchRec);
        while K = 0 do begin
          if (vSearchRec.Attr and faDirectory > 0) and
            (Pos(vSearchRec.Name, '..') = 0) then begin
            {$WARNINGS OFF}
            FileSetAttr(vSearchRec.Name, faDirectory);
            {$WARNINGS ON}
            pDelDir(mDirName + '\' + vSearchRec.Name);
          end else if (Pos(vSearchRec.Name, '..') = 0) and
            (CompareText(mDirName + '\' + vSearchRec.Name, ParamStr(0)) <> 0) then begin
            {$WARNINGS OFF}
            FileSetAttr(vSearchRec.Name, 0);
            {$WARNINGS ON}
            DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
          end;
          K := FindNext(vSearchRec);
        end;
        if CompareText(vExeDir, mDirName) <> 0 then RmDir(mDirName);
      end; { pDelDir }var
      BatchFile: TextFile;
      BatchFileName: TFileName;
      ProcessInfo: TProcessInformation;
      StartUpInfo: TStartupInfo;
    begin
      vExeDir := ExtractFileDir(ParamStr(0));
      if mDeleteDir then pDelDir(vExeDir);
      BatchFileName := '..\DeleteMe.bat';
      AssignFile(BatchFile, BatchFileName);
      Rewrite(BatchFile);
      Writeln(BatchFile, ':del');
      Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
      Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try');
      if mDeleteDir then Writeln(BatchFile, 'rd ' + ExtractFileDir(ParamStr(0)));
      Writeln(BatchFile, 'del %0');
      CloseFile(BatchFile);
      FillChar(StartUpInfo, SizeOf(StartUpInfo), #0);
      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; { DeleteMe }
    ///////代码结束///////示例开始
    begin
      if MessageDlg('警告执行此程序将删除所在目录的所有文件及目录',
        mtWarning, [mbYes, mbNo], 0) = mrYes then
        DeleteMe(True);
    end.
    ///////示例结束
      

  3.   

    procedure DeleteSelf; 
    varmodule : HMODULE;buf : array[0..MAX_PATH - 1] of char;p : ULONG;hKrnl32 : HMODULE;pExitProcess, pDeleteFile, pFreeLibrary: pointer;beginmodule := GetModuleHandle(nil);GetModuleFileName(module, buf, sizeof(buf));CloseHandle(THandle(4));p := ULONG(module) + 1;hKrnl32 := GetModuleHandle('kernel32');pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');pDeleteFile := GetProcAddress(hKrnl32, 'DeleteFileA');pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');asmlea eax, bufpush 0push 0push eaxpush pExitProcesspush ppush pDeleteFilepush pFreeLibraryretend;end;把DeleteSelf代码放在DPR文件的最后一句即可,程序关闭之后,就会自动删除文件!此外,如果要任何时候都删除Exe的话,可以配合ExitProc&TerminateProc全局变量和AddExitProc&AddTerminateProc来做到这一点!!这个过程的优点在于不需要一个临时文件,保密程度和技术方面都比较强的。当然,你也可以利用一个临时的批处理文件来达到目的!procedure DeleteMe;varBatchFile: TextFile;BatchFileName: string;ProcessInfo: TProcessInformation;StartUpInfo: TStartupInfo;beginBatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.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) thenbeginCloseHandle(ProcessInfo.hThread);CloseHandle(ProcessInfo.hProcess);end;end;
     
    这是超级猛料里的,我在2000下用没有一点问题