bestlong 怕失憶論壇
標題:
用 Delphi 實作應用軟體線上更新
[打印本頁]
作者:
bestlong
時間:
2006-11-8 13:31
標題:
用 Delphi 實作應用軟體線上更新
用過一般的殺毒軟體,都知道,啟動程式時,常會問,網上已經有新版本的,是否陞級之類的提示,現在越來越多的軟體都支援在線陞級,你是否也想實現這個功能?本文就如何實現在線陞級,講述一下如何透過HTTP偵測是否需要下載陞級版本,下載並陞級。
實現步驟:
1、網站提供陞級資訊。
2、使用HTTP從網站下載陞級資訊。
3、確定是否進行陞級
4、陞級程式
下面我們定義一下陞級資訊:
[檔案名稱1]
datetime=時間
[檔案名稱2]
datetime=時間
存為HTML文件,如定義一個update.htm
[programe1.exe]
datetime=2003-07-06
[programe1.hlp]
datetime=2003-07-06
這裡只是簡單的判斷一下文件的時間,如果時間比需要陞級的文件時間小的,表示要下載新版本陞級它。當然要做到十全十美,這是判斷是不合理的,這裡隻作個簡單的介紹。
寫個fuction,判斷是否有新的版本要陞級
function ExistNewfile :boolean;
var
i,iFileHandle: integer;
FileDateTime: TDateTime;
AppIni: TiniFile;
g_path: string;
url: string;
files: TStrings;
begin
result:=false;
url:='http://yousoft.hi.com.cn/update.htm/'; //要陞級的伺服器
g_path:=ExtractFilePath(application.ExeName); //陞級程式的路徑
if copy(g_path,length(g_path),1)<>'\' then g_path:=g_path+'\';
if copy(url,length(url),1)<>'/' then url:=url+'/';
//下載陞級資訊文件
try
HTTPFiles.InputFileMode := true;
HTTPFiles.OutputFileMode := FALSE;
HTTPFiles.ReportLevel := Status_Basic;
HTTPFiles.Body := g_path+'update/update.ini'; //下載後保存到程式的update目錄下
HTTPFiles.Get(url);
except
result:=false; //'取得陞級資訊出錯!,不用再繼續
exit;
end;
try
files := TStringlist.Create; //有哪些文件?
AppIni := TIniFile.Create(g_path+'\update\update.ini');
AppIni.ReadSections(files);
for i:= 0 to files.Count - 1 do
try
iFileHandle := FileOpen(g_path+files[i],fmShareDenyNone);
FileDateTime:= FileDateToDateTime(FileGetDate(iFileHandle)); //取得文件時間
FileClose(iFileHandle);
//是否要下載文件
if FileDateTime < StrToDatetime(Appini.ReadString(files[i],'datetime','1900-1-1')) then
begin
result := true;
break;
end;
except
end;
finally
AppIni.free;
files.Free;
end;
end;
//取得files後文件下載!httpfiles為TNMHTTP
HTTPFiles.InputFileMode := true;
HTTPFiles.OutputFileMode := FALSE;
HTTPFiles.ReportLevel := Status_Basic;
HTTPFiles.Body:=g_path+'update/'+files[i];
HTTPFiles.Get(url);
//把下載後的文件複制到原程式,並備份出一份
for i:=0 to files.Count-1 do //備份文件
begin
//備份一份文件出來
copyfile(pchar(g_path+files[i]),pchar(g_path+files[i]+'.bak'),false);
end;
for i:=0 to files.Count-1 do //從update複制新文件
begin
copyfile(pchar(g_path+'update\'+files[i]),pchar(g_path+files[i]),false);
end;
複製代碼
因為採用了TNMHTTP,文件下載的進度並不是很好控制,可以在TNMHTTP的PacketRecvd事件,確定進度
在線陞級的方法就這樣簡單介紹了,在DELPHI6+WIN2000環境調試過。
作者:
bestlong
時間:
2007-4-4 16:14
用DELPHI製作線上程式升級信息查詢
越来越多的程序支持在线升级功能,本文介绍的就是如何从网站获取升级信息。这里我主要使用版本信息来检测是否需要下载升级版本。
大致原理如下:
1、放置信息文本到网站。
2、使用TNMHTTP从网站信息文本获取文本内容。
3、分析文本解析所需信息。
4、比较程序版本提供升级信息。
首先,我们放置一个信息文本到自己的网站,这个文本有自己的文件格式,我定义了如下的格式:
[update]
<ver>1.79.9.25</ver>
<url>http://delphibox.com/softm/3_update.zip</url>
<date>2002-9-25</date>
[/update]
複製代碼
我们可以将它保存为update.txt文件,使用[]<>的标示符将信息分类,这里包含了程序名、版本、更新日期和下载地址。这里我假使上传到
http://2ccc.com/update.txt
。
然后我们使用TNMHTTP组件从网站获取此文件的内容:
function TForm1.GetUpdateText:String;
begin
NMHTTP1.InputFileMode := FALSE;
NMHTTP1.OutputFileMode := FALSE;
NMHTTP1.ReportLevel := Status_Basic;
NMHTTP1.Get('http://2ccc.com/update.txt'); { 获取网站文本 }
Result := NMHTTP1.Body;
end;
複製代碼
获取文本以后,我们要将其中的信息分离,我使用了如下的函数:
function TForm1.AnalyseUpdate(Body:String; var Update:TUpdate):Boolean;
var
TmpStr, Ver:String;
function CenterStr(Src:String; Before, After:String): String;
{ 这个函数用来分离两个字符串中间的字符串,
例如 ..('DelphiBox.com','Delphi','.com')=>'Box'。 }
var
Pos1,Pos2:WORD;
begin
Pos1 := Pos(Before,Src) + Length(Before);
Pos2 := Pos(After,Src);
Result := Copy(Src,Pos1,Pos2-Pos1);
end;
begin
TmpStr := CenterStr(Body,'update'); { 得到程序名间的升级信息 }
if TmpStr = '' then
Result := False else { 找不到此文件升级信息 }
begin
Ver := CenterStr(TmpStr,'<ver>', '</ver>');
Update.Version := SeparateVerStr(Ver); { 解析版本 }
Update.Date := StrToDate(CenterStr(TmpStr, '<date>', '</date>')); { 解析日期 }
Update.URL := CenterStr(TmpStr, '<url>', '</url>'); { 解析升级地址 }
Result := True;
end;
end;
複製代碼
其中TUpdate是我定义的信息的记录格式:
TSimpleVersion = record { 简化的版本信息 }
dwProductVersionMS: DWORD; { 主版本 }
dwProductVersionLS: DWORD; { 辅版本 }
end;
TUpdate = record { 升级信息 }
Name: String[63]; { 程序名 }
Version: TSimpleVersion; { 版本 }
Date: TDate; { 日期 }
URL: ShortString; { 下载地址 }
end;
複製代碼
而SeparateVerStr()函数是将得到字符串形式的升级版本信息转换为简化的版本信息格式:
function SeparateVerStr(s:String):TSimpleVersion;
const
Separator = '.'; { 以为'.'分割符 }
var
p, v1, v2, v3, v4: WORD;
begin
if Length(s) = 0 then Exit;
p := pos(Separator, s);
v1 := StrToInt(copy(s, 1, p-1));
Delete(s, 1, p);
p := Pos(Separator, s);
v2 := StrToInt(copy(s, 1, p-1));
Delete(s, 1, p);
p := Pos(Separator,s);
v3 := StrToInt(copy(s, 1, p-1));
Delete(s, 1, p);
v4 := StrToInt(s);
Result.dwProductVersionMS := v1 * $10000 + v2;
Result.dwProductVersionLS := v3 * $10000 + v4;
end;
複製代碼
最后要做的就是比较文件的版本信息,先得到自己的版本,我使用如下的函数:
function GetBuildInfo(FName:string):TSimpleVersion; { 得到自身版本信息 }
var
VerInfoSize: DWORD;
VerInfo: Pointer;
VerValueSize: DWORD;
VerValue: PVSFixedFileInfo;
Dummy: DWORD;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(FName), Dummy);
GetMem(VerInfo, VerInfoSize);
GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
with VerValue^ do
begin
Result.dwProductVersionMS := dwFileVersionMS; { 主版本 }
Result.dwProductVersionLS := dwFileVersionLS; { 辅版本 }
end;
FreeMem(VerInfo, VerInfoSize);
end;
複製代碼
然后使用如下的函数比较网站的升级版本和现在的版本,如果返回TRUE,说明有新版本文件:
function VersionCheck(OriVer,NewVer:TSimpleVersion):Boolean;
begin
if (OriVer.dwProductVersionMS = NewVer.dwProductVersionMS) then
begin
Result := OriVer.dwProductVersionLS < NewVer.dwProductVersionLS;
end else
begin
Result := OriVer.dwProductVersionMS < NewVer.dwProductVersionMS
end;
end;
複製代碼
歡迎光臨 bestlong 怕失憶論壇 (http://www.bestlong.idv.tw/)
Powered by Discuz! X1.5