可以用SHFileOperation,这是一个ShellAPI,Delphi中没有提供直接文件拷贝的函数。 要自己写Copy的函数, 程序举例:可能不是很正确,提供思路。 var F1,F2:TFile; Buf:array[0..1023] of Byte; Size:Integer; begin AssignFile(F1,'源文件名称'); AssignFile(F2,'目标文件名称'); ResetFile(F1); Rewrite(F2); Size:=1024; while not Eof(F1) do begin BlockRead(F1,Buf,Size); BlockWrite(F2,Buf,Size); end; CloseFile(F1); CloseFile(F1); end;
var FromF, ToF: file; NumRead, NumWritten: Integer; Buf: array[1..2048] of Char; begin if OpenDialog1.Execute then { Display Open dialog box } begin AssignFile(FromF, OpenDialog1.FileName); Reset(FromF, 1); { Record size = 1 } if SaveDialog1.Execute then { Display Save dialog box} begin AssignFile(ToF, SaveDialog1.FileName); { Open output file } Rewrite(ToF, 1); { Record size = 1 } Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF)) + ' bytes...'); repeat BlockRead(FromF, Buf, SizeOf(Buf), NumRead); BlockWrite(ToF, Buf, NumRead, NumWritten); until (NumRead = 0) or (NumWritten <> NumRead); CloseFile(FromF); CloseFile(ToF); end; end; end;
PChar(ExtractFilepath(Application.ExeName)+'autoexec.bat') ,
True);
要自己写Copy的函数,
程序举例:可能不是很正确,提供思路。
var
F1,F2:TFile;
Buf:array[0..1023] of Byte;
Size:Integer;
begin
AssignFile(F1,'源文件名称');
AssignFile(F2,'目标文件名称');
ResetFile(F1);
Rewrite(F2);
Size:=1024;
while not Eof(F1) do
begin
BlockRead(F1,Buf,Size);
BlockWrite(F2,Buf,Size);
end;
CloseFile(F1);
CloseFile(F1);
end;
NumRead, NumWritten: Integer;
Buf: array[1..2048] of Char;
begin
if OpenDialog1.Execute then { Display Open dialog box }
begin
AssignFile(FromF, OpenDialog1.FileName);
Reset(FromF, 1); { Record size = 1 }
if SaveDialog1.Execute then { Display Save dialog box}
begin
AssignFile(ToF, SaveDialog1.FileName); { Open output file } Rewrite(ToF, 1); { Record size = 1 }
Canvas.TextOut(10, 10, 'Copying ' + IntToStr(FileSize(FromF))
+ ' bytes...');
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
end;
end;
end;
BlueTrees(蜗牛)
ahuige(灰不遛秋) 非常感谢!