Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65841
  • 博文数量: 42
  • 博客积分: 1730
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 13:06
文章分类

全部博文(42)

文章存档

2011年(1)

2009年(41)

我的朋友

分类: C/C++

2009-09-22 17:47:55

// RecursionDelete.cpp : Defines the entry point for the console application.

//

  
#include "stdafx.h"
//#include

#include <afx.h>
#include <iostream>
using namespace std;
  
//void DeleteDir(CString szPath);

void RecursionDelete(CString szPath);
  
int _tmain(int argc, _TCHAR* argv[])
{
    CString Dirname = "D:\\test";
  
    //DeleteDir("D:\\test");

    RecursionDelete(Dirname);
    getchar();
    return 0;
}
  
void RecursionDelete(CString szPath)
{
    CFileFind ff;
    CString strPath = szPath;
  
    //look for all files

    if (strPath.Right(1) != "\\")
    {
        strPath += "\\";
    }
    strPath += "*.*";
  
    BOOL bRet;
    if (ff.FindFile(strPath))
    {
        do
        {
            bRet = ff.FindNextFile();
            if (ff.IsDots())
            {
                continue;
            }
            strPath = ff.GetFilePath();
            if (!ff.IsDirectory())
            {
                //delete the file

                ::SetFileAttributes(strPath, FILE_ATTRIBUTE_NORMAL);
                ::DeleteFile(strPath);
            }
            else
            {
                RecursionDelete(strPath);
  
                //delete the directory

                ::SetFileAttributes(strPath, FILE_ATTRIBUTE_NORMAL);
                ::RemoveDirectory(strPath);
            }
        } while (bRet);
    }
    ::SetFileAttributes(szPath, FILE_ATTRIBUTE_NORMAL);
    /*
    如果没有这句话,最根的那个目录不能删除(即main里只的test不能被删除),如果加上这句话就可以删除。
    为什么这样,还没有弄明白,还望高手指教
    */

    ff.Close();
    ::RemoveDirectory(szPath);
}

 

这里在编译的时候可能有问题,因为用到了MFC类库,所以要进行设置(vs08)

project -> properties -> project defaults ->修改use of mfc 为Use MFC in a Shared DLL

阅读(832) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~