需求:由于mfc本身不支持删除非空文件夹操作,所以需要自己封装删除文件夹操作。 1 调用系统的dos功能; 2 递归删除 //删除一个文件夹下的所有内容 void myDeleteDirectory(CString directory_path) { CFileFind finder; CString path; path.Format("%s/*.*",directory_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()); } } } void DeleteAllDirectory() //删除文件夹 包括非空的文件夹 { myDeleteDirectory("C:/ForVcTest"); RemoveDirectory("C:/ForVcTest"); } 3 删除指定扩展名文件 char sTempFileName[200]; CString type,name = sFoundFileName; type = name.Right(3).MakeUpper(); if(type == "TXT") { sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName); DeleteFile(sTempFileName); }