为什么RemoveDirectory函数没有删除最上面的文件夹

Why is RemoveDirectory function not deleting the top most folder?

本文关键字:文件夹 删除 RemoveDirectory 函数 为什么      更新时间:2023-10-16

参考:codeguru.com/forum/showthread.php?t=239271

使用以下功能删除文件夹时,除最上面的文件夹外,所有文件夹、子文件夹和文件都将被删除。假设对于路径c:folder1folder2,除了folder2之外,folder2下的所有东西都被删除。

BOOL DeleteDirectory(const TCHAR* sPath)  
{  
    HANDLE hFind; // file handle
    WIN32_FIND_DATA FindFileData;
    TCHAR DirPath[MAX_PATH];
    TCHAR FileName[MAX_PATH];
    _tcscpy(DirPath,sPath);
    _tcscat(DirPath,_T("\"));
    _tcscpy(FileName,sPath);
    _tcscat(FileName,_T("\*")); // searching all files
    int nRet = 0;
    hFind = FindFirstFile(FileName, &FindFileData); // find the first file
    if( hFind != INVALID_HANDLE_VALUE ) 
    {
        do
        {
            if( IsDots(FindFileData.cFileName) ) 
                continue; //if not directory continue
            _tcscpy(FileName + _tcslen(DirPath), FindFileData.cFileName);
            if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
            {
                // we have found a directory, recurse
                if( !DeleteDirectory(FileName) ) 
                    break;   // directory couldn't be deleted
            }
            else 
            {
                if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
                    _wchmod(FileName, _S_IWRITE); // change read-only file mode
                if( !DeleteFile(FileName) ) 
                    break;  // file couldn't be deleted
            }
        }while( FindNextFile(hFind, &FindFileData) );
        nRet = FindClose(hFind); // closing file handle
    }
    return RemoveDirectory(sPath); // remove the empty (maybe not) directory and returns zero when RemoveDirectory function fails
}  

如有任何帮助,我们将不胜感激。在调试过程中,我注意到FindClose函数成功关闭了文件句柄,但GetLastError返回32("进程无法访问该文件,因为它正被另一个进程使用")。但是,在尝试使用进程资源管理器后,我没有任何线索。

虽然您可以通过这种方式删除目录,但让系统通过调用SHFileOperation传递FO_DELETE来为您删除目录更简单。请记住,必须对传递给此API的字符串进行双null终止。

我认为您必须在递归调用之前关闭文件句柄。这意味着在退出递归调用后,您必须再次将文件句柄设置为适当的值。

SHFileOperation可能是一个更好的解决方案;我只是在回答OP的问题,为什么他们的代码没有按预期工作。

参考:http://www.codeguru.com/forum/archive/index.php/t-337897.html
下面给出的是使用SHFileOperation 删除目录的代码

bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true)
{
    int len = _tcslen(lpszDir);
    TCHAR* pszFrom = new TCHAR[len+4]; //4 to handle wide char
    //_tcscpy(pszFrom, lpszDir); //todo:remove warning//;//convet wchar to char*
    wcscpy_s (pszFrom, len+2, lpszDir);
    pszFrom[len] = 0;
    pszFrom[len+1] = 0;
    SHFILEOPSTRUCT fileop;
    fileop.hwnd   = NULL;    // no status display
    fileop.wFunc  = FO_DELETE;  // delete operation
    fileop.pFrom  = pszFrom;  // source file name as double null terminated string
    fileop.pTo    = NULL;    // no destination needed
    fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user
    if(!noRecycleBin)
        fileop.fFlags |= FOF_ALLOWUNDO;
    fileop.fAnyOperationsAborted = FALSE;
    fileop.lpszProgressTitle     = NULL;
    fileop.hNameMappings         = NULL;
    int ret = SHFileOperation(&fileop); //SHFileOperation returns zero if successful; otherwise nonzero 
    delete [] pszFrom;  
    return (0 == ret);
}