MFC對文件文件夾轉移、刪除、重命名、復制【轉】


HFileOperation()函數主要對文件夾有四種操作:復制,刪除,移動,重命名。

寫了四個函數。可以很好的對文件夾進行操作。

 

//函數名: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;

  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;

  return SHFileOperation(&FileOp) == 0;

  }


 

/////////////////////////////////////

  //函數名: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;

  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;

  return SHFileOperation(&FileOp) == 0;

  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM