代替copyfile,效率会低,少用,并且不适合大文件。
unit UnitCopy;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
ActiveX,
Comobj,
ShlObj,
TlHelp32,
Winapi.ShellAPI, Vcl.ComCtrls;
type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
ProgressBar1: TProgressBar;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Procedure myFileCopy(const SourceFile, TargetFile: string);
var
SourceF, TargetF: file;
iRead, iWrite: integer;
Buf: array[1..2048] of char;
FS: TFileStream;
isize:Integer;
begin
FS := TFileStream.Create(SourceFile, fmShareDenyNone);
isize:=FS.Size;
FS.Free;
//用ProgressBar控件
Form1.ProgressBar1.Position:=0;
Form1.ProgressBar1.Max:=isize;
Application.ProcessMessages;
AssignFile(SourceF, SourceFile);
reset(SourceF,1);
AssignFile(TargetF, TargetFile);
Rewrite(TargetF,1);
repeat
BlockRead(SourceF, Buf, sizeof(Buf), iRead);
BlockWrite(TargetF, Buf, iRead, iWrite);
Form1.ProgressBar1.Position:=Form1.ProgressBar1.Position+iWrite;
Application.ProcessMessages;
until (iRead = 0) or (iWrite <> iRead);
closefile(SourceF);
closefile(TargetF);
end;
procedure myFileCopy2(const SourceFile, TargetFile: string);
var
SourceF,TargetF: TFileStream;
num, n: Integer;
buf: PByte;
block: Integer;
begin
if FileExists(TargetFile) then
DeleteFile(TargetFile);
SourceF := TFileStream.Create(SourceFile, fmOpenRead or fmShareExclusive);
TargetF := TFileStream.Create(TargetFile, fmCreate);
num := SourceF.Size;
//TargetF.Size := num;
SourceF.Position := 0;
//TargetF.Position := 0;
block := num div 100;
GetMem(buf, block);
Form1.ProgressBar1.Max := 100;
Form1.ProgressBar1.Min := 0;
Form1.ProgressBar1.Position := 0;
while num <> 0 do
begin
Application.ProcessMessages;
n := block;
if n > num then n := num;
SourceF.ReadBuffer(buf^, n);
TargetF.WriteBuffer(buf^, n);
Form1.ProgressBar1.Position := Trunc((1 - num / SourceF.Size) * 100);
Dec(num, n);
end;
SourceF.Free;
TargetF.Free;
Form1.ProgressBar1.Position := 100;
end;
procedure CloseThread(exename:string);
var
lppe: TProcessEntry32;
snapshothandle:THandle;
f:boolean;
pid:DWord;
ProcessName:string;
begin
ProcessName:=exename;
snapshothandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
lppe.dwSize:=Sizeof(lppe);
f:=Process32First(snapshothandle,lppe);
While f do
begin
if UpperCase( ChangeFileExt(ProcessName,'') )=UpperCase( ChangeFileExt(StrPas(lppe.szExeFile),'') ) then
begin
pid:=lppe.th32ProcessID;
TerminateProcess(OpenProcess(Process_Terminate,false,pid),0);
end;
f:=Process32Next(snapshothandle,lppe);
end;
CloseHandle(snapshothandle);
end;
procedure myyanshi(haomiao: Cardinal);
var t:Int64;
begin
t:=GetTickCount64();
while GetTickCount64()-t<haomiao do
begin
Sleep(5);
application.ProcessMessages;
end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//关闭主程序,参数名进程的显示名称即exe的名称
CloseThread('数据上传.exe');
myyanshi(1000);
//复制文本并替换
DeleteFile('D:\AD未备份20230619\数据上传.exe');
CopyFile( '\192.168.0.241\ljk\数据上传.exe','D:\AD未备份20230619\数据上传.exe',False);
myyanshi(1000);
//打开
ShellExecute(self.Handle,'open',PChar('D:\AD未备份20230619\数据上传.exe'),'','',SW_SHOWNORMAL );
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
myFileCopy('C:\芯片\kang.rar','c:\kang.mp4');
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
myFileCopy2('c:\ljk.mp4','c:\kang.mp4');
end;
end.
标签:copyfile,begin,SourceF,进度条,TargetF,ProgressBar1,Form1,复制,end From: https://www.cnblogs.com/ljk2008/p/18342541