请问,在程序中copy文件时,如何知道已copy 了多少了?我用来做进度条。(100分)

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

microwave

Unregistered / Unconfirmed
GUEST, unregistred user!
有例子最好。
 
你可以用文件流啊,自己知道大小,又知道已经拷贝了多少,控制进度条最好了。
 
有没有例子?
 
给你找一个,稍等。
 
下面这段例子是我写的文件合并器用的,你看看,来不及改了。
var
FS: TFileStream;
MS: TMemoryStream;
i, FileCount: integer;
FileName, SaveFile: string;
FileList: TStrings;
begin
AFile := ReplaceAll(AFile, '"', '');
FileName := ExtractFileName(AFile);
SaveFile := ChangeFileExt(FileName, '');
FileName := ChangeFileExt(FileName, '.*');
FileList := TStringList.Create;
if SearchFile(ExtractFilePath(AFile), FileName, FileList) then
begin
FileCount := 0;
for i := 0 to FileList.Count - 1 do
begin
try
if (StrToInt(ExtName(FileList.Strings)) > FileCount) then
FileCount := StrToInt(ExtName(FileList.Strings));
except
Application.MessageBox('分割文件有误!', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
exit;
end;
end;
try
if GetStrRight(deSave.Text, 1) <> '/' then
FS := TFileStream.Create(deSave.Text + '/' + SaveFile, fmCreate)
else
FS := TFileStream.Create(deSave.Text + SaveFile, fmCreate)
except
Application.MessageBox('创建文件错误,请确认文件是否已经存在。', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
exit;
end;
for i := 1 to FileCount do
begin
MS := TMemoryStream.Create;
try
MS.LoadFromFile(ExtractFilePath(AFile) + SaveFile + CreateExtName(i));
except
Application.MessageBox('读取文件错误,请确认分割文件是否完整。', '错误', MB_OK + MB_ICONERROR);
FileList.Free;
MS.Free;
FS.Free;
exit;
end;
FS.CopyFrom(MS, MS.Size);
MS.Free;
end;
FS.Free;
end;
FileList.Free;
Application.MessageBox('合并文件成功!', '信息', MB_OK + MB_ICONINFORMATION);
end;
 
用这个就行了呀,window带的SHFileOperation
 
也是,调用现成的东西,见笑了。
 
You need a TProgressBar on your form for this tip.

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;
 
多人接受答案了。
 
后退
顶部