原文网址:http://shijuanfeng.blogbus.com/logs/100675115.htmlhtml
第一种方法:定义一个文件类对象来操做
CFile TempFile;
TempFile.Remove(指定文件名);
第二种方法: 使用系统函数 DeleteFile( LPCSTR filename )删除文件 _rmdir(),删除目录 DeleteDirectory(sTempDir); 删除目录 RemoveDirectory(sTempDir);删除目录
eg: DeleteFile( char *tempFileName); 函数
令注:若要清空文件,但保留目录用: system(“del C:\temp”); // 清空了C:\temp中的全部文件,可是不会清楚文件夹下的子目录,并且会弹出:是否删除的Doc框 htm
//删除文件夹目录(非空) 上面提到的删除目录的方法只能删除空目录(即文件夹),若是目录下有文件或者子目录,就不能删除了,VC里好像没有直接的函数,只能手动写个函数来删除了,下面提供一个删除非空目录的方法: 对象
bool DeleteDirectory(char* sDirName)
{blog
CFileFind tempFind;
char sTempFileFind[200];
strcpy(sTempFileFind, sDirName);
strcat(sTempFileFind, “\\*.*”);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
char sFoundFileName[200];
strcpy(sFoundFileName,tempFind.GetFilePath());
DeleteFile(sFoundFileName);
}
tempFind.Close();file
}方法
清空整个文件夹的内容(包括子文件夹),但保留该文件夹删除文件
void CRelCtrlDlg::DeleteDirectory(char* sDirName)
{
char sPath[200];
strcpy(sPath, sDirName);di
CFileFind ff;
BOOL bFound;
char sTempFileFind[200];
strcpy(sTempFileFind, sPath);
strcat(sTempFileFind, “\\*.*”);文件
bFound = ff.FindFile(sTempFileFind);
while(bFound)
{
bFound = ff.FindNextFile();
CString sFilePath = ff.GetFilePath();
if(ff.IsDirectory()) { if(!ff.IsDots()) DeleteDirectory((LPSTR)(LPCTSTR)sFilePath); } else { if(ff.IsReadOnly()) { SetFileAttributes(sFilePath, FILE_ATTRIBUTE_NORMAL); } DeleteFile(sFilePath); } } ff.Close(); SetFileAttributes(sPath, FILE_ATTRIBUTE_NORMAL); if (!strcmp(sPath,sDirName)) { return; } RemoveDirectory(sPath);}