本程序可以复制一个文件(夹)到指定的位置,当然,写法还很多,如使用IMAPIFolder::CopyFolder函数下面这段代码使用了SHFileOperation和CopyFile两个函数,子文件夹用前者,单个文件使用后者.
BOOL CopyFolder(CString fromPath,CString toPath)
{
CFileFind finder;
BOOL bWorking = finder.FindFile( LPCTSTR (fromPath+"//*.*") );
while(bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDots()) //dot
continue;
if(finder.IsDirectory()) //folder
{
CString folderpath = finder.GetFilePath();
char tmp[MAX_PATH];
ZeroMemory(tmp,MAX_PATH);
strcpy(tmp,folderpath.GetBuffer( folderpath.GetLength() ));
SHFILEOPSTRUCT sfo;
sfo.hwnd = NULL;
sfo.wFunc = FO_COPY;
sfo.pFrom = tmp;
sfo.pTo = toPath.GetBuffer( toPath.GetLength() );
sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR ;
SHFileOperation(&sfo);
folderpath.ReleaseBuffer();
toPath.ReleaseBuffer();
continue;
}
else //file
{
CString filename=finder.GetFileName();
CopyFile(finder.GetFilePath(),toPath+"//"+filename,false);
}
}
return TRUE;
}
注:用ZeroMemory(tmp,MAX_PATH)对路径进行初始化,因为SHFileOperation中的pFrom可以表示多个路径,各个路径之间以/0分隔,最后以/0/0即连续的两个/0)结束.