<pre name="code" class="cpp">// 功能:删除文件夹或者文件
BOOL DeleteFolder(LPCTSTR lpFilerPath)
{
WIN32_FIND_DATA FileData;
wstring strFileName;
BOOL bResult = FALSE;
HANDLE hLisFile = NULL;
__try
{
strFileName = lpFilerPath;
if ( 0 == strFileName.size())
{
bResult = FALSE;
__leave;
}
// 如果这个地址是文件,直接删除走人
if (!(FILE_ATTRIBUTE_DIRECTORY&::GetFileAttributes(lpFilerPath)))
{
::DeleteFile(lpFilerPath);
bResult = TRUE;
__leave;
}
if (3 == strFileName.size() && L'\\' == strFileName[2])
{
strFileName += L"*";
}
else
{
strFileName += L"\\*";
}
hLisFile =::FindFirstFile(strFileName.data(),&FileData);
if (INVALID_HANDLE_VALUE == hLisFile)
{
bResult = FALSE;
__leave;
}
do
{
if(!lstrcmp(FileData.cFileName,L".") || !lstrcmp(FileData.cFileName,L".."))
{
continue;
}
wstring strFilePathTemp = lpFilerPath;
strFilePathTemp += L"\\";
strFilePathTemp += FileData.cFileName;
if(FileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
// 开始递归
if(TRUE == (bResult = DeleteFolder(strFilePathTemp.data())))
{
continue;
}
else
{
__leave;
}
}
::DeleteFile(strFilePathTemp.data());
} while (::FindNextFile(hLisFile,&FileData));
::RemoveDirectory(lpFilerPath);
bResult = TRUE;
}
__finally
{
if (NULL != hLisFile)
{
::FindClose(hLisFile);
}
}
return bResult;
}
C++删除文件及文件夹(封装)
最新推荐文章于 2024-05-22 11:06:01 发布