SHFileOperation()函数主要对文件夹有四种操作:复制,删除,移动,重命名。
挤时间对这个函数进行了利用了一下。写了四个函数。可以很好的对文件夹进行操作。
/
//函数名:DeleteFolder
//输入参数:LpszPath 要删除的路径指针
//作用:删除指定文件夹以及里面的文件
//
/
bool DeleteOneFolder()
{
BOOL bRet = FALSE;
//LPCTSTR lpszPath;
TCHAR* szNewPath = new TCHAR[MAX_PATH + 2];
SecureZeroMemory(szNewPath, MAX_PATH + 2);
TCHAR* szTempPath = L"\\要删除的目录";
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, szNewPath)))
{
if (szNewPath)
{
int nLength = _tcslen(szNewPath);
if (_tcslen(szTempPath) < MAX_PATH + 2 - nLength)
{
_tcscpy_s(szNewPath + nLength, MAX_PATH + 2 - nLength, szTempPath);
nLength = _tcslen(szNewPath);
if (nLength <= MAX_PATH)
{
szNewPath[nLength] = '\0';
szNewPath[nLength+1] = '\0';
SHFILEOPSTRUCT FileOp;
SecureZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = szNewPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
bRet = SHFileOperation(&FileOp);
}
}
delete[] szNewPath;
szNewPath = NULL;
}
}
return !bRet;
}
/
//函数名: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;
}
/
//函数名: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;
}
这四个函数在VC6.0下通过测试了一下,效果还是有的。不过感觉,FO_RENAME和FO_MOVE有点相似,而且是作用十分相似
挤时间对这个函数进行了利用了一下。写了四个函数。可以很好的对文件夹进行操作。
/
//函数名:DeleteFolder
//输入参数:LpszPath 要删除的路径指针
//作用:删除指定文件夹以及里面的文件
//
/
bool DeleteOneFolder()
{
BOOL bRet = FALSE;
//LPCTSTR lpszPath;
TCHAR* szNewPath = new TCHAR[MAX_PATH + 2];
SecureZeroMemory(szNewPath, MAX_PATH + 2);
TCHAR* szTempPath = L"\\要删除的目录";
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, szNewPath)))
{
if (szNewPath)
{
int nLength = _tcslen(szNewPath);
if (_tcslen(szTempPath) < MAX_PATH + 2 - nLength)
{
_tcscpy_s(szNewPath + nLength, MAX_PATH + 2 - nLength, szTempPath);
nLength = _tcslen(szNewPath);
if (nLength <= MAX_PATH)
{
szNewPath[nLength] = '\0';
szNewPath[nLength+1] = '\0';
SHFILEOPSTRUCT FileOp;
SecureZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = szNewPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
bRet = SHFileOperation(&FileOp);
}
}
delete[] szNewPath;
szNewPath = NULL;
}
}
return !bRet;
}
/
//函数名: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;
}
/
//函数名: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;
}
这四个函数在VC6.0下通过测试了一下,效果还是有的。不过感觉,FO_RENAME和FO_MOVE有点相似,而且是作用十分相似