procedure TImportExportFrm.InputFile(sourcefile, targetfile: string);
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, SourceFile);
Reset(FromF,1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
AssignFile(ToF, TargetFile);
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);
CloseFile(FromF);
CloseFile(ToF);
end;
可能会有比这个简单的复制文件的过程,但是使用这样的方法,主要是为了使复制文件时,用进度条显示复制进度,而这是必须的!
5 个解决方案
#1
procedure TImportExportFrm.InputFile(sourcefile, targetfile: string);
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
TotalNumRead: Integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, SourceFile);
Reset(FromF,1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
TotalNumRead:=0;
repeat
BlockRead(FromF, Buf[TotalNumRead], fsize , NumRead);
TotalNumRead := TotalNumRead + NumRead;
size := size - NumRead;
until (fsize=0);
AssignFile(ToF, TargetFile);
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);
CloseFile(FromF);
CloseFile(ToF);
end;
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
TotalNumRead: Integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, SourceFile);
Reset(FromF,1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
TotalNumRead:=0;
repeat
BlockRead(FromF, Buf[TotalNumRead], fsize , NumRead);
TotalNumRead := TotalNumRead + NumRead;
size := size - NumRead;
until (fsize=0);
AssignFile(ToF, TargetFile);
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);
CloseFile(FromF);
CloseFile(ToF);
end;
#2
//*****
FileMode:=0;//只读模式打开文件
//*****
Reset(FromF,1);
...
//*****
FileMode:=2;//读\写模式打开文件
//*****
Rewrite(ToF,1);
FileMode:=0;//只读模式打开文件
//*****
Reset(FromF,1);
...
//*****
FileMode:=2;//读\写模式打开文件
//*****
Rewrite(ToF,1);
#3
我自己已经解决了,方法和上面的二位不同,我用的方法是:
FileSetAttr(sourcefile, faArchive); //添加这样一句将文件属性变为非只读
AssignFile(FromF, SourceFile);
Reset(FromF,1);
FileSetAttr(sourcefile, faArchive); //添加这样一句将文件属性变为非只读
AssignFile(FromF, SourceFile);
Reset(FromF,1);
#4
应保持源文件的属性不变啊,要不然怎么叫“复制” ^ ^
#5
我注重的是只要拷贝文件,文件的只读属性改不改没关系。
#1
procedure TImportExportFrm.InputFile(sourcefile, targetfile: string);
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
TotalNumRead: Integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, SourceFile);
Reset(FromF,1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
TotalNumRead:=0;
repeat
BlockRead(FromF, Buf[TotalNumRead], fsize , NumRead);
TotalNumRead := TotalNumRead + NumRead;
size := size - NumRead;
until (fsize=0);
AssignFile(ToF, TargetFile);
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);
CloseFile(FromF);
CloseFile(ToF);
end;
var
fsize:Longint;
FromF, ToF: file;
NumRead, NumWritten: integer;
TotalNumRead: Integer;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, SourceFile);
Reset(FromF,1);
fsize := filesize(fromf);
ProgressBar1.Max:=fsize;
TotalNumRead:=0;
repeat
BlockRead(FromF, Buf[TotalNumRead], fsize , NumRead);
TotalNumRead := TotalNumRead + NumRead;
size := size - NumRead;
until (fsize=0);
AssignFile(ToF, TargetFile);
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);
CloseFile(FromF);
CloseFile(ToF);
end;
#2
//*****
FileMode:=0;//只读模式打开文件
//*****
Reset(FromF,1);
...
//*****
FileMode:=2;//读\写模式打开文件
//*****
Rewrite(ToF,1);
FileMode:=0;//只读模式打开文件
//*****
Reset(FromF,1);
...
//*****
FileMode:=2;//读\写模式打开文件
//*****
Rewrite(ToF,1);
#3
我自己已经解决了,方法和上面的二位不同,我用的方法是:
FileSetAttr(sourcefile, faArchive); //添加这样一句将文件属性变为非只读
AssignFile(FromF, SourceFile);
Reset(FromF,1);
FileSetAttr(sourcefile, faArchive); //添加这样一句将文件属性变为非只读
AssignFile(FromF, SourceFile);
Reset(FromF,1);
#4
应保持源文件的属性不变啊,要不然怎么叫“复制” ^ ^
#5
我注重的是只要拷贝文件,文件的只读属性改不改没关系。