我这有一段删除文件夹的代码,在我用时,能删除文件夹里的文件,但删除文件夹本身时却不对,说是另一个进程正在使用这个文件?
BOOL CFileListView::DeleteDirectory(CString sDirectory)
{
    CString sPath = sDirectory;
    sPath += "\\*.*";    CFileFind finder;
    CString sFileName;
    BOOL bIsFindFile = finder.FindFile(sPath);
    while (bIsFindFile)
    {
        bIsFindFile = finder.FindNextFile();
        if (!finder.IsDots())
        {
            sFileName = finder.GetFilePath();
            if (finder.IsDirectory())
            {
                ::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
                if (!DeleteDirectory(sFileName))
                {
                    return FALSE;
                }
            }
            else
            {
                ::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
                ::DeleteFile(sFileName);
            }
        }
    }
    finder.Close();
    ::SetFileAttributes(sDirectory, FILE_ATTRIBUTE_ARCHIVE);
    if (!::RemoveDirectory(sDirectory))//这里出错。
    {
        return FALSE;
    }
    return TRUE;
}

解决方案 »

  1.   

    居然没发现你说的问题,郁闷。
    其实删除文件夹也可以这样实现:
    删除目录
    SHFILEOPSTRUCT op={0};
    op.wFunc = FO_DELETE;
    op.pFrom = "c:\\22";
    op.fFlags = FOF_SILENT|FOF_NOCONFIRMATION|FOF_ALLOWUNDO;
    SHFileOperation(&op);
      

  2.   

    void CCountyDataServerDlg::DeleteFolder(CString szPath)
    {
        CFileFind ff;
        BOOL bFound;
        bFound = ff.FindFile(szPath + "\\*.*");
        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(szPath, FILE_ATTRIBUTE_NORMAL);
        RemoveDirectory(szPath);}