我用一个临时文件夹里存放一些excel临时文件在程序结束时想删除这些临时文件如何才能实现呀!

解决方案 »

  1.   

    var
       lsPath: string;
       FileOp: TSHFileOpStruct;
    begin
          lsPath := 'c:\temp\';
          //删除临时文件
          with FileOp do begin
             Wnd := 0;
             wFunc := FO_DELETE;
             pFrom := PChar(lsPath  + '*.*' + #0);     //取目录,与isFaq没有实际关系
             pTo := nil;
             fFlags := FOF_SILENT + FOF_NOCONFIRMATION ;
             fAnyOperationsAborted := False;
             hNameMappings := nil;
             lpszProgressTitle := nil;
          end;
          SHFileOperation(FileOp);
         
          RemoveDir(lsPath );
    end;
      

  2.   

    type
        TFileClass = class
        private
          procedure CleanDirectoryProc(sFileName: string; var bContinue: Boolean);
        end;
        TEnumDirectoryFileProc = procedure (Filename: string; var bContinue: Boolean) of object;
    {===============================================================
      功  能:  删除文件或文件夹
      参  数:  sFileName 文件或文件夹名
      返回值:
      备  注:  原形
    =================================================================}
    procedure TFileClass.CleanDirectoryProc(sFileName: string; var bContinue: Boolean);
    var
      Attr: Integer;
    begin
      Attr := FileGetAttr(sFileName);
      Attr := (not faReadOnly) and Attr; // Turn off ReadOnly attribute
      Attr := (not faHidden) and Attr;   // Turn off Hidden attribute
      FileSetAttr(sFileName, Attr);  if Attr and faDirectory <> 0 then
        RMDir(sFileName)
      else
        SysUtils.DeleteFile(sFileName);
    end;{=====================================================
      功  能:  删除该目录及以下所有文件夹和文件
      参  数:  sDir 文件夹名称
      返回值:
      备  注:
    ======================================================}
    procedure FileDeleteDirectory(sDir: string);
    begin
      //if not MsgYesNoBox('确信要删除该目录及以下所有文件夹和文件吗?') then exit;
      with TFileClass.Create do
        try
          EnumDirectoryFiles(sDir, '*.*', faAnyFile, CleanDirectoryProc);
        finally
          Free;
        end;
      RMDir(sDir);
    end;{=====================================================
      功  能:  删除该目录及以下所有文件夹和文件
      参  数:  sDir 文件夹名称 SMASK/过滤文件字符串
               Attr 文件属性 EnumDirectoryFileProc 本参数为删除过程
      返回值:
      备  注:  
    ======================================================}
    procedure EnumDirectoryFiles(sDir, SMASK: string; Attr: Integer; EnumDirectoryFileProc: TEnumDirectoryFileProc);
    var
      SearchRec: TSearchRec;
      Status   : Integer;
      bContinue: Boolean;
    begin
      sDir := PathWithSlash(sDir);  // traverse child directories
      Status := FindFirst(sDir + '*.*', faDirectory, SearchRec);
      try
        while Status = 0 do
        begin
          if (SearchRec.name <> '.') and (SearchRec.name <> '..') then
            EnumDirectoryFiles(sDir + SearchRec.name, SMASK, Attr, EnumDirectoryFileProc);      Status := FindNext(SearchRec);
        end;
      finally
        SysUtils.FindClose(SearchRec);
      end;  // exam each valid file and invoke the callback func
      Status := FindFirst(sDir + SMASK, faAnyFile, SearchRec);
      try
        while Status = 0 do
        begin
          if (SearchRec.Attr and Attr <> 0) and (FileExists(sDir + SearchRec.name) or DirectoryExists(sDir + SearchRec.name)) and
            not ((SearchRec.Attr and faDirectory <> 0) and ((SearchRec.name = '.') or (SearchRec.name = '..'))) then
          begin
            bContinue := True;
            EnumDirectoryFileProc(sDir + SearchRec.name, bContinue);
            if not bContinue then Break;
          end;      Status := FindNext(SearchRec);
        end;  finally
        SysUtils.FindClose(SearchRec);
      end;
    end;
      

  3.   

    http://expert.csdn.net/Expert/topic/2494/2494130.xml?temp=.2395746
    上次回答的...
      

  4.   

    procedure maketree;
    var
      sr:Tsearchrec;
      err:integer;
      filepath:string;
    begin
      err:=findfirst('*.*',$3F,sr);
      while (err=0) do
       begin
         if sr.Name[1]<>'.' then
           begin
             if (sr.Attr and fadirectory)=0 then
             begin
               deletefile(sr.Name);
             end;
             if (sr.Attr and fadirectory)=16 then
             begin
               chdir(sr.name);
               filepath:=ExpandFileName(sr.name);           maketree;
               chdir('..');
             end;
           end;
           err:=findnext(sr);
       end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
        chdir(Edit1.text);
        maketree;
        rmdir('c:\1\1');
    end;
      

  5.   

    function DelFile(sDir,fExt: string): Boolean;
    var
       hFindFile: HWND;
       FindFileData: WIN32_FIND_DATA;
       sr: TSearchRec;
    begin
      sDir:= sDir + '\';
      hFindFile:= FindFirstFile(pchar(sDir + fExt), FindFileData);
      if hFindFile <> NULL then
        begin
          deletefile(sDir + FindFileData.cFileName);
          while FindNextFile(hFindFile, FindFileData) <> FALSE do
            deletefile(sDir + FindFileData.cFileName);
        end;
      sr.FindHandle:= hFindFile;
      FindClose(sr);
    end;