为什么RemoveDirectory函数没有删除最上面的文件夹
Why is RemoveDirectory function not deleting the top most folder?
参考:codeguru.com/forum/showthread.php?t=239271
使用以下功能删除文件夹时,除最上面的文件夹外,所有文件夹、子文件夹和文件都将被删除。假设对于路径c:folder1folder2
,除了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("进程无法访问该文件,因为它正被另一个进程使用")。但是,在尝试使用进程资源管理器后,我没有任何线索。
虽然您可以通过这种方式删除目录,但让系统通过调用SHFileOperation
传递FO_DELETE
来为您删除目录更简单。请记住,必须对传递给此API的字符串进行双null终止。
我认为您必须在递归调用之前关闭文件句柄。这意味着在退出递归调用后,您必须再次将文件句柄设置为适当的值。
SHFileOperation可能是一个更好的解决方案;我只是在回答OP的问题,为什么他们的代码没有按预期工作。
参考: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);
}
相关文章:
- 如何检测文件夹是否太重,如果是,请删除其中的一部分
- 如何使用 c++ 删除文件夹及其内容
- 在x元素之后,从QT中删除x元素之后的文件夹中的项目
- 删除每个API呼叫的文件夹都会使用ERROR_ACCESS_DENIED失败
- 删除临时文件夹的内容
- 为什么这种删除文件夹中文件的方法不起作用?
- 从文件夹中排除 cpp 而不删除
- 如何在删除或处理文件夹树时处理符号链接和连接
- 如何删除回收站中路径过长的文件夹或文件
- 删除用户 Temp 目录中的文件夹,但也要考虑 unicode 文件夹名称
- 卸载程序不会删除复制到VS2010安装程序项目中的系统文件夹。
- 如何使用 NIX 标准库删除文件夹中的所有文件,但不删除文件夹
- 在 Win32 中删除子文件夹本身之前,如何从文件夹和子文件夹中删除文件
- 使用 c++ 创建/删除文件夹
- 在调用 cmake 之前删除构建文件夹中的所有内容是否合理
- 为什么RemoveDirectory函数没有删除最上面的文件夹
- 如何以阻止的方式删除文件/文件夹
- In code-删除包含文件的文件夹
- C++代码,使用FindFirstFile/FindNextFile删除文件夹中的所有文件
- 无法删除 C++ 中的空文件夹