用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧Delphi

删除程序自身

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:20:10
(*//
标题:自己删除自己
说明:警告执行此程序将删除所在目录的所有文件及目录
设计:Zswang
日期:2002-01-29
支持:wjhu111@21cn.com
//*)

///////Begin Source
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 }
///////End Source

///////Begin Demo
begin
  if MessageDlg(''警告执行此程序将删除所在目录的所有文件及目录'',
    mtWarning, [mbYes, mbNo], 0) = mrYes then
    DeleteMe(True);
end.
///////End Demo


这个绝对没有问题
我用得好好的,98/2000/XP都OK
procedure DeleteSelf;
var
  Batchfile: TextFile;
BatchFileName: string;
  ProcessInfo: TProcessInformation;
  StartUpInfo: TStartupInfo;
begin
  BatchFileName := ChangeFileExt(Paramstr(0),''.bat'');
  AssignFile(BatchFile, BatchFileName);
  Rewrite(BatchFile);
  // build cmd batch file
  Writeln(BatchFile, '':try'');
  Writeln(BatchFile, Format(''del "%s"'', [ParamStr(0)]));
  Writeln(BatchFile, Format(''if exist "%s" goto try'', [ParamStr(0)]));
  Writeln(BatchFile, ''del %0'');
  CloseFile(BatchFile);
  FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
  StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartUpInfo.wShowWindow := SW_HIDE;
  // create hidden process
  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;

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:6,921.87500 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号