|
轉(zhuǎn)自http://www./news/1007/444817.html SHFileOperations刪除操作,pFrom接受變量傳值時(shí)老是出錯(cuò),搞了一晚上沒(méi)查出原因,還好查到這位同學(xué)的資料,手工在字符串后面加2個(gè)'\0',就可以了,如果是直接手寫(xiě)路徑,微軟庫(kù)自動(dòng)作此處理了。 SHFileOperation()函數(shù)主要對(duì)文件夾有四種操作:復(fù)制,刪除,移動(dòng),重命名。 擠時(shí)間對(duì)這個(gè)函數(shù)進(jìn)行了利用了一下。寫(xiě)了四個(gè)函數(shù)??梢院芎玫膶?duì)文件夾進(jìn)行操作。 ///////////////////////////////////// //函數(shù)名:DeleteFolder //輸入?yún)?shù):LpszPath 要?jiǎng)h除的路徑指針 //作用:刪除指定文件夾以及里面的文件 // ///////////////////////////////////// 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; } ///////////////////////////////////// //函數(shù)名:CopyFolder //參數(shù):lpszFromPath 源文件夾的路徑 。 lpszToPath 目的文件夾的路徑 //作用:拷貝文件夾及其文件夾中的所有內(nèi)容 // ////////////////////////////////////// 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; } ///////////////////////////////////// //函數(shù)名:MoveFolder //參數(shù):lpszFromPath 源文件夾路徑 。lpszToPath 目的文件夾路徑 //作用:移動(dòng)原文件夾及其中文件都指定的路徑下 // ///////////////////////////////////// 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 //參數(shù):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; } 這四個(gè)函數(shù)在VC6.0下通過(guò)測(cè)試了一下,效果還是有的。不過(guò)感覺(jué),F(xiàn)O_RENAME和FO_MOVE有點(diǎn)相似,而且是作用十分相似。 |
|
|