📄 update1.pas
字号:
unit update1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls,inifiles, RzStatus,math,
IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP, RealOneProgressBar, Buttons;
type
TUpdateForm = class(TForm)
Panel1: TPanel;
Timer1: TTimer;
IdHTTP: TIdHTTP;
IdAntiFreeze1: TIdAntiFreeze;
Timer2: TTimer;
Image1: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
NowFilePrg: TRealOneProgressBar;
TotalPrg: TRealOneProgressBar;
Message_Title: TLabel;
Label4: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure UpdateBegin(Sender:TObject);
procedure IdHTTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCountMax: Integer);
procedure IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
procedure DownLoadFile(localfile:string;webfile:string);
procedure SpeedButton1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
supdate, dupdate: TDateTime; //本机升级日期,升级包更新日期
end;
var
UpdateForm: TUpdateForm;
Finished:Bool;
implementation
{$R *.dfm}
function GetFileSize(const fn:string):LongInt;
Var
theFile:file of byte;
size:LongInt;
begin
assignfile(theFile,fn);
reset(theFile);
size:=filesize(theFile);
result:=size;
closefile(theFile);
end;
procedure TUpdateForm.DownLoadFile(localfile:string;webfile:string);
var
temhttp: TIdHTTP;
tStream:Tfilestream;
begin
temhttp := TIdHTTP.Create(nil);
with temhttp do
begin
OnWorkBegin:=IdHTTPWorkBegin;
OnWork:=IdHTTPWork;
Request.ContentRangeStart :=0;
end;
if FileExists(localfile) then //如果文件已经存在
tStream := TFileStream.Create(localfile, fmOpenWrite)
else
tStream := TFileStream.Create(localfile, fmCreate);
try
temhttp.Get(webfile, tStream); //开始下载
finally
freeandnil(tstream);
temhttp.Disconnect;
end;
end;
procedure TUpdateForm.UpdateBegin(Sender:TObject);
var myini:Tinifile;
updatepath,downloadpath,downfilecount:string;
i:integer;
totalcent:Real;
downlist,updatetobat:TStrings;
locfile,webfile:string;
begin
try
Finished:=False;
updatepath:=ExtractFilePath(application.ExeName)+'Update\';
if FileExists(updatepath+'update.ver') then
begin
myini:=Tinifile.Create(updatepath+'update.ver');
downloadpath:='http://'+myini.ReadString('Update','ip','www.zt123.net')+'/'+myini.ReadString('Update','directory','update')+'/';
downlist:=TStringList.Create;
updatetobat:=TStringList.Create;
updatetobat.Clear;
updatetobat.Add('@echo off');
updatetobat.Add('@ping 127.0.0.1 -n 2 >nul');
downlist.Clear;
myini.ReadSection('downfiles',downlist);
for i:=0 to downlist.Count -1 do
begin
downfilecount:='('+inttostr(i+1)+'/'+inttostr(downlist.Count)+')';
Message_Title.Caption:=' '+downfilecount+' 正在下载文件:'+myini.ReadString('downfiles',downlist[i],'')+' ...';
{开始下载文件}
IdAntiFreeze1.OnlyWhenIdle:=False; //设置使程序有反应.
locfile:=ExtractFilePath(application.ExeName)+myini.ReadString('downfiles',downlist[i],'')+'_';
webfile:=downloadpath+myini.ReadString('downfiles',downlist[i],'');
DownLoadFile(locfile,webfile);
totalcent:=(i/downlist.Count)*100;
updatetobat.Add('@del '+myini.ReadString('downfiles',downlist[i],''));
updatetobat.Add('@ren '+myini.ReadString('downfiles',downlist[i],'')+'_'+' '+myini.ReadString('downfiles',downlist[i],''));
TotalPrg.Position:=ceil(totalcent);
application.ProcessMessages;
end;
Label1.Caption:='新版本已下载,正在安装新版本...';
Message_Title.Caption:='下载完成。';
Finished:=True;
TotalPrg.Position:=100;
myini.Free;
downlist.free;
if FileExists(ExtractFilePath(paramstr(0))+'Temp.bat') then DeleteFile(ExtractFilePath(paramstr(0))+'Temp.bat');
try
updatetobat.Add('@ping 127.0.0.1 -n 1 >nul');
updatetobat.Add(Application.ExeName);
updatetobat.Add('@del Temp.bat');
updatetobat.SaveToFile(ExtractFilePath(paramstr(0))+'Temp.bat');
finally
updatetobat.Free;
end;
application.ProcessMessages;
sleep(2000);
hide;
application.Terminate;
winexec(Pchar(ExtractFilePath(paramstr(0))+'Temp.bat'),sw_hide);
end
except
application.MessageBox('升级过程中出现异常,请重新运行程序~!','错误',MB_ICONERROR);
application.Terminate;
end;
end;
procedure TUpdateForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=Canone;
end;
procedure TUpdateForm.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled:=False;
Timer2.Enabled:=True;
UpdateBegin(Sender);
end;
procedure TUpdateForm.IdHTTPWorkBegin(Sender: TObject;
AWorkMode: TWorkMode; const AWorkCountMax: Integer);
begin
NowFilePrg.Max := AWorkCountMax;
NowFilePrg.Min := 0;
NowFilePrg.Position := 0;
end;
procedure TUpdateForm.IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
NowFilePrg.Position := AWorkCount;
end;
procedure TUpdateForm.SpeedButton1Click(Sender: TObject);
begin
if Finished=False then
begin
if application.MessageBox('程序正在升级中,现在退出将中断正在下载的文件,继续吗?','提示',MB_ICONQUESTION+MB_YESNO)=IDYES then
begin
halt;
end;
end
else
application.Terminate;
end;
procedure TUpdateForm.FormDestroy(Sender: TObject);
begin
IDHttp.Free;
end;
procedure TUpdateForm.Timer2Timer(Sender: TObject);
begin
application.BringToFront;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -