您的位置:首页 > 编程语言

MFC 删除一个文件夹下的所有文件代码

2016-12-22 16:25 465 查看
/* CString to char */
char* CStringToChar(CString szStr)
{
int len = WideCharToMultiByte(CP_ACP,0,szStr,-1,NULL,0,NULL,NULL);
if (len == 0)
{
return NULL;
}

char* buffer = new char[len+1];

WideCharToMultiByte(CP_ACP,0,szStr,-1,buffer,len,NULL,NULL);

return buffer;
}

bool DeleteDirectory(string strDirName)
{
TypeConversion type;
CFileFind tempFind;
char strTempFileFind[MAX_PATH];

sprintf(strTempFileFind,"%s\\*.*", strDirName.c_str());

BOOL IsFinded = tempFind.FindFile(CString(strTempFileFind));
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();

if (!tempFind.IsDots())
{
char* strFoundFileName = CStringToChar(tempFind.GetFileName().GetBuffer(MAX_PATH));

if (tempFind.IsDirectory())
{
char strTempDir[MAX_PATH];
sprintf(strTempDir,"%s\\%s", strDirName, strFoundFileName);
DeleteDirectory(strTempDir);
}
else
{
char strTempFileName[MAX_PATH];
sprintf(strTempFileName,"%s\\%s", strDirName.c_str(), strFoundFileName);
DeleteFile(CString(strTempFileName));
}
}
}
tempFind.Close();

if(!RemoveDirectory(CString(strDirName.c_str())))
{
return FALSE;
}
return TRUE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: