Procedure TForm1.CopyFileWithProgressBar(Source, Destination : string); 
var 
  FromF, 
  ToF        : file of byte; 
  Buffer     : array[0..4096] of char; 
  NumRead    : integer; 
  FileLength : longint; 
begin 
  AssignFile(FromF,Source); 
  reset(FromF); 
  AssignFile(ToF,Destination); 
  rewrite(ToF); 
  FileLength:=FileSize(FromF); 
  With Progressbar1 do 
  begin 
    Min := 0; 
    Max := FileLength; 
    while FileLength > 0 do 
    begin 
      BlockRead(FromF,Buffer[0],SizeOf(Buffer),NumRead); 
      FileLength := FileLength - NumRead; 
      BlockWrite(ToF,Buffer[0],NumRead); 
      Position := Position + NumRead; 
    end; 
    CloseFile(FromF); 
    CloseFile(ToF); 
end; 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  CopyFileWithProgressBar('c:\Windows\Welcome.exe','c:\temp\Welcome.exe'); 
end; 

解决方案 »

  1.   

    是的,你千万不能用api函数进行拷贝,只能自己编写程序拷贝,调用api它实际上会另外一个进程
      

  2.   

    将文件以一块一块的方式进行拷贝这样你就可以
    控制他的进度了:
    方法如下,你先试试:
    program BlockFileCopy(S_Filename,D_Filename:string)
    var
      S_File,D_File:file;
      NumRead,NumWritten:Word;
      Buf:array[1..500] of Char;//块的大小
    begin
      assginFile(S_File,S_Filename);
      reset(S_File,1);//注意
      AssignFile(D_File,D_Filename) ;
      rewrite(D_file,1);//
      repeat
        BlockRead(S_file,Buf,Sizeof(Buf),NumRead);
        BlockWrite(D_File,buf,sizeof(Buf),NumWritten);
      //在此加入你的进度处理
      Until (NumRead=0) or (NumWritten<>NumRead);
      CloseFile(S_File);
      CloseFile(D_File);
    end;