Why is RemoveDirectory function not deleting the top most folder?

后端 未结 3 1176
故里飘歌
故里飘歌 2020-12-18 12:11

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

When using the function below to delete folders, all folders, subfolders and files are getting deleted except for

3条回答
  •  不知归路
    2020-12-18 12:32

    Refer:http://www.codeguru.com/forum/archive/index.php/t-337897.html
    Given below is the code to delete directory using 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);
    }
    

提交回复
热议问题