请问Delphi中哪一个函数可用于删除文件夹。

解决方案 »

  1.   

    用API  SHFileOperation以下是VB中的一段说明
    Private Type SHFILEOPSTRUCT
            hwnd As Long
            wFunc As Long       '对文件的操作指令
            pFrom As String     '源文件或路径
            pTo As String       '目的文件或路径
            fFlags As Integer   '操作标志
            fAnyOperationsAborted As Long
            hNameMappings As Long
            lpszProgressTitle As String
    End TypePrivate Declare Function SHFileOperation Lib _
            "shell32" _
            (lpFileOp As SHFILEOPSTRUCT) As Long
      

  2.   

    用SHFileOperation
    不管目录是否为空均可删除,而且可以设定是否放入回收站
      

  3.   

    用这个function DelDirectory(const Source:string): boolean;
    var
      fo: TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
      begin
        Wnd := 0;
        wFunc := FO_DELETE;
        pFrom := PChar(source+#0);
        pTo := #0#0;
        fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
      end;
      Result := (SHFileOperation(fo) = 0);
    end;
      

  4.   

    function JudgeDir(Attr:integer):boolean;
          {判断是否是目录}
          var
          i:integer;
          begin
          i:=Attr; if i>=32 then i:=i-32; //排除文档文件
          if i>=16
          then Result:=true
          else Result:=false; //返回是否是目录
          end;      function DelTree(Dir:string):integer;
          {删除整个目录,含出错处理,返回值为出错的文件数目}
          var
          Sr:TSearchRec; Err,ErrorFile,i:integer;
          CurFilePath,TempFilePath:string;
          begin
          ErrorFile:=0; //初始化错误文件数
          CurFilePath:=Dir; TempFilePath:=CurFilePath; //初始化
          Err:=FindFirst(Dir+'\*.*',$37,Sr); //查找第一个文件
          while (Err = 0) do
          begin
          if Sr.Name[1]<>'.' //判断特殊目录"."和".."
          then begin
          if JudgeDir(Sr.Attr)
          then begin //处理目录情况
          TempFilePath:=CurFilePath; //保存当前目录
          CurFilePath:=CurFilePath+'\'+Sr.Name;
          i:=DelTree(CurFilePath); //递归调用
          if i<>0 then ErrorFile:=ErrorFile+i-1;
          ChDir('..'); //返回上一级目录
          if not RemoveDir(CurFilePath)
          then ErrorFile:=ErrorFile+1; //删除目录
          CurFilePath:=TempFilePath; //恢复当前目录
          end
          else begin //处理文件情况
          if not DeleteFile(CurFilePath+'\'+Sr.Name)
          then ErrorFile:=ErrorFile+1;
          end;
          end;
          Err:=FindNext(Sr); //查找下一个文件或目录
          end;
          ChDir('..'); //返回总目录
          if not RemoveDir(Dir) then ErrorFile:=ErrorFile+1;
          //处理无法删除总目录
          Result:=ErrorFile; //返回出错的文件数目
          end;
      

  5.   

    可否据一个SHFileOperation的例子,谢谢!!!