www.gusucode.com > 高级文件属性操作的VC例子代码源码程序 > 高级文件属性操作的VC例子代码源码程序\code\FileOP.cpp
//Download by http://www.NewXing.com // FileOP.cpp: implementation of the CFileOP class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "FileExample.h" #include "FileOP.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CFileOP::CFileOP() { } CFileOP::~CFileOP() { } BOOL CFileOP::Copy(CString strSourcePath, CString strTargetPath, int nType) { switch (nType) { case COPY_CFILE: return CopyByCFile(strSourcePath, strTargetPath); case COPY_API: return CopyByApi(strSourcePath, strTargetPath); } return false; } BOOL CFileOP::CopyByCFile(CString strSourcePath, CString strTargetPath) { CFile fSource, fTarget; //定义4k字节的缓冲区 char c[4096]; int nCount; //打开文件 if (!fSource.Open(strSourcePath, CFile::modeRead)) { AfxMessageBox("Open Source File Fail!"); return false; } if (!fTarget.Open(strTargetPath, CFile::modeCreate | CFile::modeWrite)) { AfxMessageBox("Create Target File Fail!"); return false; } //读文件到缓冲区c nCount = fSource.Read(c, 4096); while (nCount) { fTarget.Write(c, nCount); nCount = fSource.Read(c, 4096); } fSource.Close(); fTarget.Close(); return true; } BOOL CFileOP::CopyByApi(CString strSourcePath, CString strTargetPath) { CFileFind f; SHFILEOPSTRUCT fop; memset(&fop, 0x00, sizeof(SHFILEOPSTRUCT)); if (!f.FindFile(strSourcePath)) return false; strSourcePath += '\0'; fop.hwnd = NULL; fop.pFrom = strSourcePath; fop.pTo = strTargetPath; fop.wFunc = FO_COPY; fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; return !::SHFileOperation(&fop); }