OnDestroy,这也不能解决根本问题

解决方案 »

  1.   

    ◇[DELPHI]如何清空一个目录 
    function EmptyDirectory(TheDirectory :String ; Recursive : Boolean) :
    Boolean;
    var
    SearchRec : TSearchRec;
    Res : Integer;
    begin
    Result := False;
    TheDirectory := NormalDir(TheDirectory);
    Res := FindFirst(TheDirectory + '*.*', faAnyFile, SearchRec);
    try
    while Res = 0 do
    begin
    if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
    begin
    if ((SearchRec.Attr and faDirectory) > 0) and Recursive
    then begin
    EmptyDirectory(TheDirectory + SearchRec.Name, True);
    RemoveDirectory(PChar(TheDirectory + SearchRec.Name));
    end
    else begin
    DeleteFile(PChar(TheDirectory + SearchRec.Name))
    end;
    end;
    Res := FindNext(SearchRec);
    end;
    Result := True;
    finally
    FindClose(SearchRec.FindHandle);
    end;
    end;◇[DELPHI]目录完全删除(deltree)
    procedure TForm1.DeleteDirectory(strDir:String); 
    var 
    sr: TSearchRec; 
    FileAttrs: Integer; 
    strfilename:string; 
    strPth:string; 
    begin 
    strpth:=Getcurrentdir(); 
    FileAttrs := faAnyFile; 
    if FindFirst(strpth+'\'+strdir+'\*.*', FileAttrs, sr) = 0 then 
    begin 
    if (sr.Attr and FileAttrs) = sr.Attr then 
    begin 
    strfilename:=sr.Name; 
    if fileexists(strpth+'\'+strdir+'\'+strfilename) then 
    deletefile(strpth+'\'+strdir+'\'+strfilename); 
    end; 
    while FindNext(sr) = 0 do 
    begin 
    if (sr.Attr and FileAttrs) = sr.Attr then 
    begin 
    strfilename:=sr.name; 
    if fileexists(strpth+'\'+strdir+'\'+strfilename) then 
    deletefile(strpth+'\'+strdir+'\'+strfilename); 
    end; 
    end; 
    FindClose(sr); 
    removedir(strpth+'\'+strdir); 
    end; 
    end;
      

  2.   

    将OnClose中的语句写在这里试试看。
    程序最后那一刹将激发该事件
    procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    beginend;
      

  3.   

    不行不行,肯定不行。
    我要执行的BAT文件是要删除程序本身。在CloseQuery事件中就执行,太提前了,不行的。
      

  4.   

    试试重载form或application的BeforeDestruction或析构函数。。在那里面进行处理
      

  5.   

    用线程吧.删除程序的线程监视主程序的CLOSE事件,触发CLOSE事件就发信给删除程序,删除程序收信后就执行.当然你应该在程序一开始就让删除程序常驻内存以便监视CLOSE事件.
      

  6.   

    我认为application还活着,bat怎么能删除?关键在于让application terminal而不是sleep N天
      

  7.   

    不行,我觉得重载application的卸构函数可能可以
      

  8.   

    我让他sleep是为了等它做完他该做的。因为我的启动BAT语句是放在最后,这在我的一个简单测试程序里是可以的。
    我想的一个问题是,为什么在我那个测试程序中启动BAT语句可以删除自己呢?因为启动BAT文件的一瞬,程序并没有结束,是BAT语句启动到执行的那段时间,程序结束了,被删除了吗?是这样吗?不知道。对我现在这个程序的困惑是,我把启动BAT文件语句放到了最后,按理说不管我的程序在生存期间做了什么,都该很顺利的和我的测试程序一样删除掉自己,可事实并不是这样,所以我想,在close事件中,程序还执行了其他操作,可我该怎么抓住删除自己的良好时机呢?
      

  9.   

    firetoucher(蹈火者),你的想法似乎很好,可我不知道该怎么做,能获得什么。希望多多指点。
      

  10.   

    在Finalization里处理试一试。Whoo
      

  11.   

    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    ===========================在。dpr中加
    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, '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;
      // 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;

    这里是关键:
      ...
      Application.Run;
      DeleteSelf;
    end.
    绝对保证可以自我删除!!!!!!!!!!!!!!!!1
    ===========================
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
    自我删除?超级容易的,一定要给我50分的!!!!!
      

  12.   

    ly_liuyang(Liu Yang) ,大哥,你看清楚点好吗?
    自我删除我已经实现了,但问题是……算了,自己看吧,这50分啊,您还是多加把劲再说吧。
      

  13.   

    再写一个程序,程序关闭时通知windows击活这个程序(不是调用)然后这个程序用陷阱反复删除