3

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

使用以下功能删除文件夹时,除了最上面的文件夹外,所有文件夹、子文件夹和文件都将被删除。说路径c:\folder1\folder2下的所有东西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(“该进程无法访问该文件,因为它正在被另一个进程使用”)但是在尝试使用进程资源管理器后我不知道。

4

3 回答 3

6

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

于 2012-03-13T12:41:59.977 回答
2

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

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

于 2012-08-28T17:36:17.920 回答
1

参考: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);
}
于 2012-03-13T13:25:20.127 回答