用Delphi實現軟體的線上升級

來源:互聯網
上載者:User

用過一般的殺毒軟體,都知道,啟動程式時,常會問,網上已經有新版本的,是否升級之類的提示,現在越來越多的軟體都支援線上升級,你是否也想實現這個功能?本文就如何?線上升級,講述一下如何通過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://bianceng.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環境調試通過

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.