复制文件
大哥大姐们,这么在vc.net怎样复制文件啊。
最好说详细点,小弟初学.net,理解能力不好
[解决办法]
DWORD CCopyFileDlg::MyCopyFile(char * source, char * dest)
{
HANDLE hSource = 0, hDest = 0;
hSource = CreateFile(source,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
if (hSource == INVALID_HANDLE_VALUE) return 1;
hDest = CreateFile(dest, GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
if (hDest ==INVALID_HANDLE_VALUE)
{
CloseHandle(hSource);
return 1;
}
try
{
BYTE Buffer[102400]; //100k
int BufSize = sizeof(Buffer);
DWORD ReadVal = 0, WriteVal = 0;
memset(Buffer, 0, BufSize);
if (!ReadFile(hSource, Buffer, BufSize, &ReadVal, NULL)) return 2;
while (ReadVal > 0)
{
if ((!WriteFile(hDest, Buffer, ReadVal, &WriteVal, NULL)) || (ReadVal != WriteVal))
return 3;
if (!ReadFile(hSource, Buffer, BufSize, &ReadVal, NULL))
return 2;
}
CloseHandle(hSource); //正常copy完文件后关闭文件
CloseHandle(hDest);
}
catch (...)
{
CloseHandle(hSource);
CloseHandle(hDest);
}
return 0;
}
调用:
if (MyCopyFile( "C:\\a.rmvb ", "C:\\b.rmvb ") != 0)
AfxMessageBox( "Error ");
else
AfxMessageBox( "拷贝成功 ");
上面的代码是不用copyfile的方法,自己实现