您当前的位置:delphi教程 > Delphi笔记 >  Delphi中实现文件拷贝的方法 显示copy文件时的进度条

Delphi中实现文件拷贝的方法 显示copy文件时的进度条

Delphi中实现文件拷贝的三种方法

1.调用API函数 
procedure CopyFile(FromFileName,ToFileName:string);
var
f1,f2:file;
Begin
AssignFile(f1,FromFileName); file://指定源文件名
AssignFile(f2,ToFileName); file://指定目标文件名
Reset(f1);
Try
Rewrite(f2);
Try
If Lzcopy(TfileRec(f1).handle,TfileRec(f2).Handle)<0
Then
Raise EinoutError.creat('文件复制错误')
Finally
CloseFile(f2); file://关闭 f2
End;
Finally
Until length(sLine)<=0;
End;
End;

2.文件流

procedure copyfile;
var f1,f2: tfilestream ;
begin
f1:=Tfilestream.Create(sourcefilename,fmopenread);
try
f2:=Tfilestream.Create(targetfilename,fmopenwrite or fmcreate);
try
f2.CopyFrom(f1,f1.size);
finally
f2.Free;
end;
finally
f1.Free;
end;
end; 
3.利用内存块读写buffer实现

Procudure FileCopy(const Fromfile,Tofile:string);
Var
F1,F2:file;
NumRead,Numwritten:word;
Buf:array [1..2048] of char;
Begin
AssignFile(F1,Fromfile);
Reset(F1,1);
AssignFile(F2,Tofile);
Rewrite(F2,1);
Repeat
BlockRead(F1,buf,sizeof(buf),NumRead);
BlockWrite(F2,buf,Numread,NumWritten);
Until (NumRead=0) or (NumWritten<>NumRead);
CloseFile(F1);
CloseFile(F2);
End;


如何显示copy文件时的进度条(使用copyfileex)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function CopyProgress(
  TotalFileSize         : LARGE_INTEGER;  // total file size, in bytes
  TotalBytesTransferred : LARGE_INTEGER;  // total number of bytes transferred
  StreamSize            : LARGE_INTEGER;  // total number of bytes for this stream
  StreamBytesTransferred: LARGE_INTEGER;  // total number of bytes transferred for this stream
  dwStreamNumber        : DWORD;          // the current stream
  dwCallbackReason      : DWORD;          // reason for callback
  hSourceFile           : THANDLE;        // handle to the source file
  hDestinationFile      : THANDLE;        // handle to the destination file
  lpData                : Pointer         // passed by CopyFileEx
  ) : DWORD; stdcall;
var
  S : String;
begin
  Form1.ProgressBar1.Position := Form1.ProgressBar1.Position+1;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  Cancel    : Bool;
  Src, Dest : String;
  hf : HFile;
  ofs : OFSTRUCT;
  size : Cardinal;
begin
  Cancel := False;
  Src := 'j:\[大富翁论坛离线资料].DelphiBBSchm.rar';
  Dest:= 'e:\1.rar';
  hf := OpenFile(PChar(src), ofs, 0);
  size := GetFileSize(hf, nil);
//  ShowMessage(IntToStr(size));
  self.ProgressBar1.Max := (size shr 16)+2;
  self.ProgressBar1.Position := 0;
  _lclose(hf);
  CopyFileEx(PChar(Src), PChar(Dest), @CopyProgress, nil, @Cancel,COPY_FILE_FAIL_IF_EXISTS)
end;

end. 


  


查看回复
关于我们 | 网站地图 | 广告刊登 | 友情链接
Copyright ©2008 - 2010  Delphi2007.net  ,All Rights Reserved  粤ICP备09038647号
业务联系:allceoad#gmail.com QQ:20008835 最佳分辨率 1024×768