Using Delphi to realize the online upgrade of software

Source: Internet
Author: User
Tags datetime ini

Use the general anti-virus software, all know, start the program, often ask, the Internet has a new version, whether or not to upgrade such tips, now more and more software support online upgrades, do you want to achieve this function? This article on how to achieve online upgrades, tell me how to detect the need to download the upgrade through HTTP, download and upgrade.

Implementation steps:

1, the website provides the promotion information.

2, use HTTP to download the upgrade information from the website.

3, determine whether to upgrade

4. Upgrade procedure

Below we define the upgrade information:

[FileName 1]

Datetime= time

[FileName 2]

Datetime= time

Save as an HTML file, such as defining a update.htm

[Programe1.exe]

datetime=2003-07-06

[Programe1.hlp]

datetime=2003-07-06

Here is simply a time to judge the file, if the time is smaller than the need to upgrade the file time, means to download a new version upgrade it. Of course, to achieve perfection, this is the judgment is unreasonable, here only to make a simple introduction.

Write a fuction to determine if there is a new version to upgrade

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/'; The server to be upgraded
G_path:=extractfilepath (application. EXEName); The path of the upgrade program
if Copy (G_path,length (G_path), 1) <> ' then g_path:=g_path+ ';
If Copy (Url,length (URL), 1) <> '/' then url:=url+ '/';
//Download upgrade information file
Try
Httpfiles.inputfilemode: = true;
Httpfiles.outputfilemode: = FALSE;
Httpfiles.reportlevel: = Status_basic;
httpfiles.body:=g_path+ ' Update/update.ini ';//Save to the program's update directory after downloading
Httpfiles.get (URL);
except
Result:=false;//' Get upgrade info Error! Without continuing the
exit;
End
Try
Files:=tstringlist.create;//What files are there?
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)); Get file Time
FileClose (ifilehandle);
Do you want to download the file
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;

Get files after file download! Httpfiles for Tnmhttp

HTTPFiles.InputFileMode := true;
HTTPFiles.OutputFileMode := FALSE;
HTTPFiles.ReportLevel := Status_Basic;
HTTPFiles.Body:=g_path+'update/'+files[i];
HTTPFiles.Get(url);

Copy the downloaded file to the original program and back up a copy

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;

Because of the adoption of tnmhttp, the file download progress is not very good control, can be in Tnmhttp PACKETRECVD events, to determine the progress of the online upgrade method so simply introduced, in the DELPHI6+WIN2000 environment debugging through

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.