procedure ApplicationUpdate();
var ExeName:String;
tmpName:String;
CurrentVersion:string;
ServerVersion:String;
FS:TFileStream;
begin
exeName:='FAQ.exe';
tmpName:='~FAQ.exe';
if ExtractFileName(application.ExeName)=exeName then
begin
//启动的是旧文件
if fileexists(extractFilePath(application.ExeName)+tmpName) then deletefile(extractFilePath(application.ExeName)+tmpName)
end;
if ExtractFileName(application.ExeName)=tmpName then
begin
//启动的是临时文件
if fileexists(extractFilePath(application.ExeName)+exeName) then deletefile(extractFilePath(application.ExeName)+exeName);
copyfile(pansichar(extractFilePath(application.ExeName)+tmpName),pansichar(extractFilePath(application.ExeName)+exeName),true);
shellexecute(application.Handle,'open',pansichar(extractFilePath(application.ExeName)+exeName),nil,nil,SW_SHOW);
application.Terminate;
end;
with dm.Adoqry_temp do
begin
//查询服务端的版本号信息
close;
sql.Text:='select version from product where id in (select max(id) from product)';
open;
if RecordCount=0 then exit;
CurrentVersion:=FileVersion(application.ExeName);
ServerVersion:=fieldbyname('version').Value;
close;
//如果版本不一致,就开始下载文件
if CurrentVersion<>ServerVersion then
begin
if application.MessageBox(Pansichar('此程序版本:'+currentVersion+#13+'服务器版本:'+serverVersion+#13+'点击"确定"进行更新'),'系统检测到新版本',MB_OKCANCEL)=ID_CANCEL then exit;
dm.adoqry_temp.SQL.Text:='select [file] from product where id in (select max(id) from product)'; dm.Adoqry_temp.Open;
FS:=TFileStream.Create(ExtractFilePath(application.ExeName)+tmpName,fmCreate);
LoadFromImage(dm.Adoqry_temp.FieldByName('file'),FS);
FS.Free;
dm.adoqry_temp.close;
//下载完后开始运行新文件并关闭本程序
shellexecute(application.Handle,'open',pansichar(extractFilePath(application.ExeName)+tmpName),nil,nil,SW_HIDE);
application.terminate;
end;
end;
end;
~FAQ.exe可通过从数据库中下载或通过文件共享的方式来获得,具体采用哪种方式就是你们自己的事喽。有什么问题可以提出来,有什么不明白直接问我,我懒得写注释。也许代码不够优化。呵呵
反正我就是通过种方式来实现的自我更新,不需借用第三个程序来处理
客户端嵌入SQL,偶一直认为是相当的不妥……
其他的不发表意见
如果不嵌入SQL的话,要怎么处理,我说过是用CS模式的嘛,既然是单个文件自我更新,有必要搞那么麻烦吗?
个人看法,仅供参考