我用递归做了一个删除目录和目录下文件,子目录的例子,子目录和文件删掉了可是根目录却删不掉

解决方案 »

  1.   

    不贴出代码怎么知道为什么删不掉,可以参考用CFileFind写的删除目录函数,没发现这种问题void DeleteFolder(CString sPath)
    {
        CFileFind ff;
        BOOL bFound;
        bFound = ff.FindFile(sPath + "\\*.*");
        while(bFound)
        {
            bFound = ff.FindNextFile();
            CString sFilePath = ff.GetFilePath();        if(ff.IsReadOnly())
            {
                SetFileAttributes(sFilePath, FILE_ATTRIBUTE_NORMAL);
            }        if(ff.IsDirectory())
            {
                if(!ff.IsDots())
                    DeleteFolder(sFilePath);
            }
            else
            {
                DeleteFile(sFilePath);
            }
            
        }
        RemoveDirectory(sPath);
    }void CDddd1Dlg::OnButton1() 
    {
        DeleteFolder("c:\\testdir");
        AfxMessageBox("done");
    }
      

  2.   

    d:\aaa\
    在aaa下还有个文件夹bbb,用上面的代码,可以删除文件,bbb里的文件也能删除,可bbb文件夹删不掉
      

  3.   

    oh,是有点问题,改正一下void DeleteFolder(CString sPath)
    {
        CFileFind ff;
        BOOL bFound;
        bFound = ff.FindFile(sPath + "\\*.*");
        while(bFound)
        {
            bFound = ff.FindNextFile();
            CString sFilePath = ff.GetFilePath();        if(ff.IsDirectory())
            {
                if(!ff.IsDots())
                    DeleteFolder(sFilePath);
            }
            else
            {
                if(ff.IsReadOnly())
                {
                    SetFileAttributes(sFilePath, FILE_ATTRIBUTE_NORMAL);
                }
                DeleteFile(sFilePath);
            }
            
        }
        ff.Close();
        SetFileAttributes(sPath, FILE_ATTRIBUTE_NORMAL);
        RemoveDirectory(sPath);
    }
      

  4.   

    void RecursiveDelete(CString szPath)
    {
    CFileFind ff;
    CString path=szPath;

    if(path.Right(1)!="\\")
    path+="\\";
    path+="*.*";
    BOOL res=ff.FindFile(path);
    while(res)
    {
    res=ff.FindNextFile();
    if(!ff.IsDots() && !ff.IsDirectory())
    DeleteFile(ff.GetFilePath());
    else if(ff.IsDots())
    continue;
    else if(ff.IsDirectory())
    {
    path=ff.GetFilePath();
    RecursiveDelete(path);
    RemoveDirectory(path);
    }
    }
    RemoveDirectory(szPath);
    }
      

  5.   

    关于删除目录(当然包括文件),我见过这么干的: SHFILEOPSTRUCT op={0};
    op.wFunc = FO_DELETE;
    op.pFrom = "c:\\aaa\\bbb";
    op.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_ALLOWUNDO;
    SHFileOperation(&op);