如何用ProgressBar显示文件COPY中的进程(50分)

  • 主题发起人 主题发起人 mylwzop
  • 开始时间 开始时间
M

mylwzop

Unregistered / Unconfirmed
GUEST, unregistred user!
我写出了COPY文件的程序,但是不知道在哪加上若干条语句 可以显示COPY时候的进程
代码如下:
procedure TForm1.FileCopy2(const FromFile, ToFile: string);
var
FromF, ToF: file;
NumRead, NumWritten: integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, FromFile);
Reset(FromF, 1);
//fsize := filesize(fromf);
AssignFile(ToF, ToFile);
Rewrite(ToF, 1);
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
System.CloseFile(FromF);
System.CloseFile(ToF);
end;
 
为什么不用API呢?
用CopyFileEx的话,它的第三个参数可以用来指定一个回调函数,从中可以得到进度信息。
你可以试试,不过我可没用过。[8D]
 
你可以考滤用流操作来实现文件拷贝
 
KAO 老资料中 讲得 太明白了[:(]
 
[^]老资料在哪?
 
看这个

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, 'temp.dbf');
AssignFile(DestFile, 'c:/temp.dbf');
// Open the source file for read access.
Reset(SrcFile, 1);
try
// Open destination file for write access.
Rewrite(DestFile, 1);
try
{ Encapsulate this into a try..except so that we can erase the file if
an error occurs. }
try
// Initialize total bytes read to zero.
TotalRead := 0;
// Obtain the filesize of the source file
FSize := FileSize(SrcFile);
{ Read SizeOf(Buffer) bytes from the source file
and add these bytes to the destination file. Repeat this
process until all bytes have been read from the source
file. A progress bar is provided to show the progress of the
copy operation. }
prbcopy.min:=0;
prbcopy.max:=trunc(fsize/500);
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 :=prbcopy.position+1;
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;
 
在循环开始前设置ProgressBar的最大值为文件大小,
在循环体中加入ProgressBar.StepBy(NumRead).
 
我试了一下,可以的。如下:
procedure TForm1.FileCopy2(const FromFile, ToFile: string);
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, FromFile);
Reset(FromF, 1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
AssignFile(ToF, ToFile);
Rewrite(ToF, 1);
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
ProgressBar1.Position:=ProgressBar1.Position+sizeof(buf);
application.ProcessMessages;
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
System.CloseFile(FromF);
System.CloseFile(ToF);
ShowMessage('OK');
end;
 
to gotosea:
谢谢指教 但是我试了 对于容量小的文件 进度条根本没有反映 对于大的文件 进度条跑的又太快了 还没复制完就到头了
 
就是一个算法问题,多试几遍就会好的!
 
这就没法子了,电脑太快了,你可以将Buf: array[1..2048] of Char再改小点,ProgressBar1的
Step改为1,但这样没多大意思,只不过是给别人一种感觉而已,何必在这些方面花费太多?
 
但是我见过别人做成功了 做程序就是要做好啊
 
:mylwzop,我那样做难道你没有试试吗?要知道,我做了2年一直都是这样做的呀。
好象这样是很简单的实现你要达到的要求呀。
 
to :一剑封喉
你的程序我也试过了 有一点你发现了吗 当你COPY 1K大的文件时 进度条会走吗?
 
不是1K是1BYTE的时候
 
后退
顶部