删除目录的函数仅在调试完成 c++ 后将其删除

Function to remove directory removes it only after debug finishes c++

本文关键字:c++ 删除 函数 调试 删除目录      更新时间:2023-10-16

我在 c++ 中有这段代码来删除包含文件的目录:

void*  hFind = INVALID_HANDLE_VALUE;
        WIN32_FIND_DATA ffd;
        hFind = FindFirstFile((fullpath+"\" + _docname + "\"+"*").c_str(), &ffd);
        do //delete all the files in the directory
        {
            // check if it is a file
            if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                string s = (fullpath+_docname+"\").append(ffd.cFileName);
                remove(s.c_str());
            }
        }
        while (FindNextFile(hFind, &ffd) != 0);
        removeDirectory(fullpath+"\" + _docname);      
        FindClose(hFind);

问题是 - 只有在我关闭 dubuger 后,该目录实际上才会被删除。调试时,目录无法访问,但仍然存在,这让我很麻烦。您知道如何修复它以删除文件夹吗?

交换最后两行可能会解决此问题:在删除目录之前关闭句柄

FindClose( hFind );
removeDirectory( fullpath + "\" + _docname );