怎样在局域网中拷贝(复制)文件(50分)

  • 主题发起人 主题发起人 bigbug
  • 开始时间 开始时间
B

bigbug

Unregistered / Unconfirmed
GUEST, unregistred user!
有两台机子,COMPU1和COMPU2(计算机名),在COMPU1的C:/TEXT/SOURCE/子目录(在COMPU2中共享名为COMMON,只读共享)下,有一个文件TEST.DB,我想把他拷贝到COMPU2的D:/DESTINATION/子目录,文件名不变,我找到不少类似问题,但对于路径都没说清楚,如copyfile(pchar(souredir),pchar(destidir),false).要求在后台操作,前台不要任何信息和人工干预.另外,这里网络质量太差,网络邻居经常找不到,机器启动后影射盘符也不是每次都能成功:-(
 
CopyFile('//COMPU1/COMMON/TEST.DB','D:/DESTINATION/TEST.DB',False);
 
uses shellapi

procedure TForm1.Button1Click(Sender: TObject);
var
opstruc:tshfileopstruct;
frombuf,tobuf:array[0..128] of char;
begin
fillchar(frombuf,sizeof(frombuf),0);
fillchar(tobuf,sizeof(tobuf),0);
//假设把d:/mpg目录下的所有文件拷贝到c:/abc目录下
strcopy(frombuf,'d:/mpg/*.*');
//mkdir('c:/abc');//创建目录c:/abc ,但会有因目录已存在而导致出错的可能,可随机创建(如根据系统时间)
strcopy(tobuf,'c:/abc/');
with opstruc do
begin
wnd:=handle;
wfunc:=fo_copy;
pfrom:=@frombuf;
pto:=@tobuf;
fflags:=fof_noconfirmation or fof_renameoncollision;
fanyoperationsaborted:=false;
hnamemappings:=nil;
lpszprogresstitle:=nil;
end;
shfileoperation(opstruc);
messagedlg('拷贝完毕!',mtinformation,[mbok],0);
end;


 
方法一:
procedure Copying;
var
FromStream,ToStream:TFileStream;
Step:integer;
begin
FromStream:=TFileStream.Create('//COMPU1/COMMON/TEST.DB',
fmOpenRead and fmShareDenyWrite);
ToStream:=TFileStream.Create('D:/DESTINATION/',fmCreate or fmOpenWrite);
Step:=100;//或 Step:=FromStream.Size div 10;
while (ToStream.Position+Step)<=(FromStream.Size) do
begin
ToStream.CopyFrom(FromStream,Step);
ToStream.CopyFrom(FromStream,FromStream.Size-ToStream.Size);
end;
ToStream.CopyFrom(FromStream,FromStream.Size-ToStream.Size);
FromStream.Destroy;
ToStream.Destroy;
end;

方法二:

CopyFile('//COMPU1/COMMON/TEST.DB','D:/DESTINATION/TEST.DB',False);

 
以上例子就该可以了吧!
最好不要在执行以上程序时,在其机器上进行拔号上网。
 
接受答案了.
 

Similar threads

回复
0
查看
1K
不得闲
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
887
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
946
DelphiTeacher的专栏
D
后退
顶部