vc使用SHFileOperation()的使用对文件夹的复制,删除,移动,重命名

23 篇文章 0 订阅

SHFileOperation()函数主要对文件夹有四种操作:复制,删除,移动,重命名。

  挤时间对这个函数进行了利用了一下。写了四个函数。可以很好的对文件夹进行操作。

  /

  //函数名:DeleteFolder

  //输入参数:LpszPath 要删除的路径指针

  //作用:删除指定文件夹以及里面的文件

  //

  /

  BOOL DeleteFolder(LPCTSTR lpszPath)

  {

  int nLength = strlen(lpszPath);

  char *NewPath = new char[nLength+2];

  strcpy(NewPath,lpszPath);

  NewPath[nLength] = '\0';

  NewPath[nLength+1] = '\0';

  SHFILEOPSTRUCT FileOp;

  ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));

  FileOp.fFlags = FOF_NOCONFIRMATION;

  FileOp.hNameMappings = NULL;

  FileOp.hwnd = NULL;

  FileOp.lpszProgressTitle = NULL;

  FileOp.pFrom = NewPath;

  FileOp.pTo = NULL;

  FileOp.wFunc = FO_DELETE;

       if (NewPath) {

           delete NewPath;

           NewPath = NULL;

       }

  return SHFileOperation(&FileOp) == 0;

  }

  /

  //函数名:CopyFolder

  //参数:lpszFromPath 源文件夹的路径 。 lpszToPath 目的文件夹的路径

  //作用:拷贝文件夹及其文件夹中的所有内容

  //

  //

  BOOL CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)

  {

  int nLengthFrm = strlen(lpszFromPath);

  char *NewPathFrm = new char[nLengthFrm+2];

  strcpy(NewPathFrm,lpszFromPath);

  NewPathFrm[nLengthFrm] = '\0';

  NewPathFrm[nLengthFrm+1] = '\0';

  SHFILEOPSTRUCT FileOp;

  ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));

  FileOp.fFlags = FOF_NOCONFIRMATION ;

  FileOp.hNameMappings = NULL;

  FileOp.hwnd = NULL;

  FileOp.lpszProgressTitle = NULL;

  FileOp.pFrom = NewPathFrm;

  FileOp.pTo = lpszToPath;

  FileOp.wFunc = FO_COPY;

        if (NewPathFrm) {

           delete NewPathFrm;

           NewPathFrm= NULL;

       }

  return SHFileOperation(&FileOp) == 0;

  }

  /

  //函数名:MoveFolder

  //参数:lpszFromPath 源文件夹路径 。lpszToPath 目的文件夹路径

  //作用:移动原文件夹及其中文件都指定的路径下

  //

  /

  BOOL MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)

  {

  int nLengthFrm = strlen(lpszFromPath);

  char *NewPathFrm = new char[nLengthFrm+2];

  strcpy(NewPathFrm,lpszFromPath);

  NewPathFrm[nLengthFrm] = '\0';

  NewPathFrm[nLengthFrm+1] = '\0';

  SHFILEOPSTRUCT FileOp;

  ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));

  FileOp.fFlags = FOF_NOCONFIRMATION ;

  FileOp.hNameMappings = NULL;

  FileOp.hwnd = NULL;

  FileOp.lpszProgressTitle = NULL;

  FileOp.pFrom = NewPathFrm;

  FileOp.pTo = lpszToPath;

  FileOp.wFunc = FO_MOVE;

        if (NewPathFrm) {

           delete NewPathFrm;

           NewPathFrm= NULL;

       }

  return SHFileOperation(&FileOp) == 0;

  }

  /

  //ReNameFolder

  //参数:lpszFromPath 源文件夹路径 。lpszToPath 目的文件夹路径

  //作用:修改原文件夹的名字。

  //

  /

  BOOL ReNameFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)

  {

  int nLengthFrm = strlen(lpszFromPath);

  char *NewPathFrm = new char[nLengthFrm+2];

  strcpy(NewPathFrm,lpszFromPath);

  NewPathFrm[nLengthFrm] = '\0';

  NewPathFrm[nLengthFrm+1] = '\0';

  SHFILEOPSTRUCT FileOp;

  ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));

  FileOp.fFlags = FOF_NOCONFIRMATION ;

  FileOp.hNameMappings = NULL;

  FileOp.hwnd = NULL;

  FileOp.lpszProgressTitle = NULL;

  FileOp.pFrom = NewPathFrm;

  FileOp.pTo = lpszToPath;

  FileOp.wFunc = FO_RENAME;

        if (NewPathFrm) {

           delete NewPathFrm;

           NewPathFrm= NULL;

       }

  return SHFileOperation(&FileOp) == 0;

  }

  这四个函数在VC6.0下通过测试了一下,效果还是有的。不过感觉,FO_RENAME和FO_MOVE有点相似,而且是作用十分相似。

 

转载自:http://www.educity.cn/develop/457810.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值