我调用系统函数:RemoveDirectory删除文件,但是空文件夹就不删除,为什么啊?

解决方案 »

  1.   

    The RemoveDirectory function deletes an existing empty directory. BOOL RemoveDirectory(    LPCTSTR lpPathName  // address of directory to remove  
       );
     ParameterslpPathNamePoints to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory.  Return ValuesIf the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError. 你的目录里可能有隐藏的文件或者你不具有删除的权限。
    你可以用 delphi 的 removedir 函数再试试。
      

  2.   

    procedure UpdateGISFile();begin
    end;procedure MakeTree();
    var  Sr : TSearchRec;
         Err : integer;
         TrSize, FilePath : string;
         sourceFileName,DecFileName :string;
         deleteDir:string;
    Begin
     Err:=FindFirst('*.*',$37,Sr) ;
     While (Err = 0) do
      begin
       if Sr.Name[1]<>'.' then
       begin
         FilePath:=ExpandFileName(Sr.Name);
         TreeSize:=TreeSize+Sr.Size;
         TrSize:=FloatToStr(TreeSize);
         updatefrm.statusLab.Caption:=IntToStr(TreeCount)
                       +'  files and folders    Size: '+TrSize;
         if (Sr.Attr and faDirectory)=0 then
            begin
              FilesSize:=FilesSize+Sr.Size;
              inc(FilesCount);
              //**************************************
               updatefrm.memo1.Lines.Add(CurrnetDir+'\'+Sr.Name);
               sourceFileName := FilePath;
              if  CurrnetDir<>'' then
                 begin
                    if Not DirectoryExists(Apppath+CurrnetDir) then
                        CreateDir (Apppath+CurrnetDir);
                    DecFileName :=  Apppath+CurrnetDir+'\'+Sr.Name;
                 end
              else
                 DecFileName :=  Apppath+Sr.Name;
               filecopy(sourceFileName,DecFileName);
               FileSetAttr(sourceFileName,0);
               DeleteFile(sourceFileName);
              //**************************************
            end;
         inc(TreeCount);
       end;   { Begin Recursion }
       If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then
       begin
         DirsSize:=DirsSize+Sr.Size;
         inc(DirsCount);
         ChDir(Sr.Name) ;
         CurrnetDir:=Sr.Name;
         MakeTree ;
         ChDir('..') ;
         CurrnetDir:='';
       end ;
       { End Recursion }
       Err:=FindNext(Sr) ;
      end ;
      deleteDir :=GetCurrentDir;
      updatefrm.Memo1.Lines.Add(deleteDir);
      if  RemoveDirectory(pchar(deleteDir))then
         updatefrm.Memo1.Lines.Add('删除成功!');
    End;我知道那个删除函数是有用的,但是我把它放在我这里就不行,可以帮忙看看为什么吗?
    它删除空文件夹的时候不包错,也不执行。
      

  3.   

    用RemoveDir代替RemoveDirectory也不行
      

  4.   

    uses shellapifunction DelDirectory(const Dir:string): boolean;
    var
      fo:TSHFILEOPSTRUCT;
    begin
      FillChar(fo, SizeOf(fo), 0);
      with fo do
        begin
          Wnd := 0;
          wFunc := FO_DELETE;
          pFrom := PChar(dir+#0);
          pTo := #0#0;
          fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
        end;
      Result:=(SHFileOperation(fo)=0);
    end;
      

  5.   

    lzf1010(深宇),你好,呵呵你写的这个是移入回收站吧,我已经 用过了,他提示:没有权限或者文件夹正在使用(应该是后者),而且我另写一个程序准备删除那个空文件夹,在我原来这个程序没有运行的时候就可以删除,但是如果原来这个程序运行一次,执行了删除操作一次就不行了,说明这个程序在删除空文件夹的时候还在占用,但是不知道怎么解决,有没有思路帮忙解决一下。
      

  6.   

    我贴的那段代码是将一个文件夹目录下的文件全部删除,文件夹下还有一层文件夹,当里面的文件被删除后,文件夹就无法删除了,但是我在删除了里面的文件后,就用CHDIR转移目录,再去删除那个空的文件夹,还是不行