MFC我用DeleteFile为什么报错

maninblack101 2016-09-25 05:44:32
代码如下:
void CFileCleanDlg::OnBnClickedButtonDelete()
{
CString path1, path2, path3;
path1 = "d:\\APP";
CFile Tempfile;
myDeleteDirectory(path1);
// TODO: 在此添加控件通知处理程序代码
}


void CFileCleanDlg::myDeleteDirectory(CString m_path)
{
CFileFind finder;
CString path;
path.Format("%s/*.*", path);
BOOL bWorking = finder.FindFile(path);
while (bWorking){
bWorking = finder.FindNextFile();
if (finder.IsDirectory() && !finder.IsDots()){//处理文件夹
myDeleteDirectory(finder.GetFilePath()); //递归删除文件夹
RemoveDirectory(finder.GetFilePath());
}
else{//处理文件
DeleteFile(finder.GetFilePath());
}
}
}
...全文
457 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xielxy 2019-01-31
  • 打赏
  • 举报
回复
单步调试看每条语句执行后的结果,就能发现问题了,代码是不会骗人的
  • 打赏
  • 举报
回复
同意楼上的,路径增加上\\*.*
zgl7903 2016-09-26
  • 打赏
  • 举报
回复
path.Format( _T("%s\\*.*"), m_path); 把文件路径TRACE出来看看 GetLastError 看错误信息
三岁、就很帅 2016-09-26
  • 打赏
  • 举报
回复
加断点看调试信息,应该就可以找到出错的那句了 看下参数值就知道了
schlafenhamster 2016-09-26
  • 打赏
  • 举报
回复
另外这种 delete 不是放 垃圾箱, 很难恢复!
schlafenhamster 2016-09-26
  • 打赏
  • 举报
回复
改了改

//
BOOL myDeleteDirectory(CString newPath)
{
	CString search=newPath;
	search +="\\*.*";
	CFileFind finder;
	BOOL bWorking = finder.FindFile(search);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		if (finder.IsDirectory() && !finder.IsDots())
		{//处理文件夹  
			//afxDump << finder.GetFilePath() << "\n";
			myDeleteDirectory(finder.GetFilePath()); 
		}
		else
		{//处理文件  
			//afxDump << finder.GetFilePath() << "\n";
			DeleteFile(finder.GetFilePath());
		}
	}
	return RemoveDirectory(newPath);
}
// del a fold
void CxxxxDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	CString root;
	root = "APP";
	BOOL ret=myDeleteDirectory(root);
	if(!ret) AfxMessageBox(root+"Delete Failed!");
}
syy64 2016-09-26
  • 打赏
  • 举报
回复
finder.GetFilePath(),看看里面的值是什么?
oyljerry 2016-09-25
  • 打赏
  • 举报
回复
先从最里面开始删除。同时查看一下出错时的返回错误码是什么
schlafenhamster 2016-09-25
  • 打赏
  • 举报
回复
参考:

/**************************************************************************

   DeleteFolder()

**************************************************************************/

BOOL DeleteFolder(HWND hWnd, LPSTR lpszFolder)
{
char              szFile[MAX_PATH];
SHFILEOPSTRUCT    fos;
WIN32_FIND_DATA   FindData;
HANDLE            hFind;
BOOL              bFindFile = TRUE;

//we can't remove a directory that is not empty, so we need to empty this one

lstrcpy(szFile, lpszFolder);
lstrcat(szFile, "\\*.*");

ZeroMemory(&fos, sizeof(fos));
fos.hwnd = hWnd;
fos.wFunc = FO_DELETE;
fos.fFlags = FOF_SILENT | FOF_ALLOWUNDO;   //send to the recycle bin

hFind = FindFirstFile(szFile, &FindData);

while((INVALID_HANDLE_VALUE != hFind) && bFindFile)
   {
   if(*(FindData.cFileName) != '.')
      {
      //copy the path and file name to our temp buffer
      lstrcpy(szFile, lpszFolder);
      lstrcat(szFile, "\\");
      lstrcat(szFile, FindData.cFileName);
      //add a second NULL because SHFileOperation is looking for this
      lstrcat(szFile, "\0");

      //delete the file
      fos.pFrom = szFile;
      SHFileOperation(&fos);
      }

   //find the next file
   bFindFile = FindNextFile(hFind, &FindData);
   }

FindClose(hFind);   

return RemoveDirectory(lpszFolder);
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧