第一种方法:定义一个文件类对象来操作

CFile   TempFile;   

  TempFile.Remove(指定文件名); 

第二种方法:  使用系统函数 DeleteFile( LPCSTR filename )删除文件    _rmdir(),删除目录 DeleteDirectory(sTempDir);  删除目录 RemoveDirectory(sTempDir);删除目录

eg:  DeleteFile(   char   *tempFileName);   

令注:若要清空文件,但保留目录用: system(“del  C:\temp”); // 清空了C:\temp中的所有文件,但是不会清楚文件夹下的子目录,而且会弹出:是否删除的Doc框 

//删除文件夹目录(非空) 上面提到的删除目录的方法只能删除空目录(即文件夹),如果目录下有文件或者子目录,就不能删除了,VC里好像没有直接的函数,只能手动写个函数来删除了,下面提供一个删除非空目录的方法: 

bool DeleteDirectory(char* sDirName)

{

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();

}

清空整个文件夹的内容(包括子文件夹),但保留该文件夹

void CRelCtrlDlg::DeleteDirectory(char* sDirName)

{

 char sPath[200];

 strcpy(sPath, sDirName);

 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);

}