Detect and Download a new version of (an own) application? uses{...,}IniFiles, UrlMon,typeTForm1 = class(TForm){...}privatewinsc: TiniFile;old: Integer;vernfo: TIniFile;end; implementation{$R *.dfm}function DownloadFile(Source, Dest: string): Boolean;{ Function for Downloading the file found on the net }begintryResult := UrlDownloadToFile(nil, PChar(Source), PChar(Dest), 0, nil) = 0;exceptResult := False;end;end;function GetPathPath: string;{ Retrive app path }beginResult := ExtractFilePath(Application.ExeName);end;procedure TForm1.DownLoadNewVersion1Click(Sender: TObject);varapath: string;new: Integer;begin// This is the exact code from my applicationapath := GetPathPath;Gauge1.Progress := 0;StatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'Connecting to http://tsoft.home.ro';Gauge1.Progress := 20;if DownloadFile('http://www.tsoft.home.ro/update.ini', PChar(apath) + '/update.ini') thenbeginGauge1.Progress := 50;StatusBAr1.SimplePanel := True;StatusBar1.SimpleText := 'Checking for newer versions...';vernfo := TiniFile.Create(GetPathPath + '/update.ini');new := vernfo.ReadInteger('version', 'wsc', 7);vernfo.Free;if (old = new) thenbeginStatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'No new version detected';Gauge1.Progress := 100;endelse if DownloadFile('http://www.tsoft.home.ro/winsafe.exe',PChar(apath) + '/winsafe.exe') thenbeginShowMessage('Update succeseful');Gauge1.Progress := 100;winsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));winsc.WriteInteger('wsc', 'vernfo', new);winsc.Free;endelseMessageDlg('A new version has appeard but it requires a second install',mtInformation, [mbOK], 0);endelsebeginStatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'Failed to connect probably a internet problem';Gauge1.Progress := 0;end;end;procedure TForm1.FormCreate(Sender: TObject);begin//App versionwinsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));tryold := winsc.ReadInteger('wsc', 'vernfo', 1);finallywinsc.Free;end;end;end.{The concept is very simple u download a ini file from your website that containsthe version you compare it with the one from your computer and it downloads thefile if the versions are Not equal if you find any froblems write me.} 来自超级猛料2003
然后就是简单的数据传输和接受,在运行响应命令。
LiveUpdate.exe查询服务器上是否有新文件方法1:
将本地的文件名列表、相应的版本号传到服务器上,比较版本号的高低方法2;
从服务器上获取最新的文件名列表、相应的版本号,与本地文件名、版本号进行比较发现有,则提醒,下载到默认的目录:Windows\Temp 或 Update目录下,然后才替换原来的文件
(防止下载不完全,而破坏了原来的文件)
在替换前,将原来的文件备份到某个目录下:Backup (防止某些文件正在使用,而替换不全,影响使用)
一般来说
方法2 是比较好的,
不影响服务器端的效率,
uses{...,}IniFiles, UrlMon,typeTForm1 = class(TForm){...}privatewinsc: TiniFile;old: Integer;vernfo: TIniFile;end; implementation{$R *.dfm}function DownloadFile(Source, Dest: string): Boolean;{ Function for Downloading the file found on the net }begintryResult := UrlDownloadToFile(nil, PChar(Source), PChar(Dest), 0, nil) = 0;exceptResult := False;end;end;function GetPathPath: string;{ Retrive app path }beginResult := ExtractFilePath(Application.ExeName);end;procedure TForm1.DownLoadNewVersion1Click(Sender: TObject);varapath: string;new: Integer;begin// This is the exact code from my applicationapath := GetPathPath;Gauge1.Progress := 0;StatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'Connecting to http://tsoft.home.ro';Gauge1.Progress := 20;if DownloadFile('http://www.tsoft.home.ro/update.ini', PChar(apath) + '/update.ini') thenbeginGauge1.Progress := 50;StatusBAr1.SimplePanel := True;StatusBar1.SimpleText := 'Checking for newer versions...';vernfo := TiniFile.Create(GetPathPath + '/update.ini');new := vernfo.ReadInteger('version', 'wsc', 7);vernfo.Free;if (old = new) thenbeginStatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'No new version detected';Gauge1.Progress := 100;endelse if DownloadFile('http://www.tsoft.home.ro/winsafe.exe',PChar(apath) + '/winsafe.exe') thenbeginShowMessage('Update succeseful');Gauge1.Progress := 100;winsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));winsc.WriteInteger('wsc', 'vernfo', new);winsc.Free;endelseMessageDlg('A new version has appeard but it requires a second install',mtInformation, [mbOK], 0);endelsebeginStatusBar1.SimplePanel := True;StatusBar1.SimpleText := 'Failed to connect probably a internet problem';Gauge1.Progress := 0;end;end;procedure TForm1.FormCreate(Sender: TObject);begin//App versionwinsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));tryold := winsc.ReadInteger('wsc', 'vernfo', 1);finallywinsc.Free;end;end;end.{The concept is very simple u download a ini file from your website that containsthe version you compare it with the one from your computer and it downloads thefile if the versions are Not equal if you find any froblems write me.}
来自超级猛料2003