1

我在 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);

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

4

1 回答 1

4

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

FindClose( hFind );
removeDirectory( fullpath + "\\" + _docname );       
于 2012-05-31T11:05:48.840 回答