• 版本

VS2012

 

  • 源代码
#include <string>

using namespace std;





/*
    删除文件夹以及它里面的所有文件或文件夹
*/
bool RemoveDir(const char* szFileDir)
{
    std::string strDir = szFileDir;
    if (strDir.at(strDir.length() - 1) != '\\')
        strDir += '\\';
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile((strDir + "*.*").c_str(), &wfd);
    if (hFind == INVALID_HANDLE_VALUE)
        return false;
    do
    {
        if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if (stricmp(wfd.cFileName, ".") != 0 &&
                stricmp(wfd.cFileName, "..") != 0)
                RemoveDir((strDir + wfd.cFileName).c_str());
        }
        else
        {
            DeleteFile((strDir + wfd.cFileName).c_str());
        }
    } while (FindNextFile(hFind, &wfd));
    FindClose(hFind);
    RemoveDirectory(szFileDir);
    return true;
}

 

  • 演示

VC++-删除文件夹以及它里面的所有文件或文件夹_删除文件夹

 

Caesar卢尚宇

2021年2月21日