害得我找光盘,补完:
方法二:
...
type
TMainForm = class(TForm)
prbCopy: TProgressBar;
btnCopy: TButton;
procedure btnCopyClick(Sender: TObject);
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.btnCopyClick(Sender: TObject);
var
SrcFile, DestFile: File;
BytesRead, BytesWritten, TotalRead: Integer;
Buffer: array[1..500] of byte;
FSize: Integer;
begin
{ Assign both the source and destination files to their
respective file variables }
AssignFile(SrcFile, 'C:/1.mdb'); //打开源文件
AssignFile(DestFile, 'C:=1.bak');//设定目标文件
Reset(SrcFile, 1);//定位到源文件头
try
Rewrite(DestFile, 1);//定位到目标文件头
try
try
TotalRead := 0;//初始化记数器
FSize := FileSize(SrcFile); //取源文件大小
repeat
BlockRead(SrcFile, Buffer, SizeOf(Buffer), BytesRead);//块读源文件
if BytesRead > 0 then //读到东东了
begin
//写目标文件
BlockWrite(DestFile, Buffer, BytesRead, BytesWritten);
if BytesRead <> BytesWritten then
//出错处理
raise Exception.Create('Error copying file')
else begin
TotalRead := TotalRead + BytesRead;
//改变进度
prbCopy.Position := Trunc(TotalRead / Fsize) * 100;
prbCopy.Update;
end;
end
until BytesRead = 0; //直到读完!
except
{ On an exception, erase the destination file as it may be
corrupt. Then re-raise the exception. }
Erase(DestFile);
raise;
end;
finally
CloseFile(DestFile); // Close the destination file.
end;
finally
CloseFile(SrcFile); // Close the source file.
end;
end;