2008. március 31., hétfő

Detect and download a new version


Problem/Question/Abstract:

How can I detect and download a new version of my applications.

Answer:

{Well first you need a home page or a server and then you must use the following source code}

uses URLMON, INIFILES;
{This will download the necesary files}

function DownloadFile(Source, Dest: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
  except
    Result := False;
  end;
end;

function GetPathPath: string;
begin
  Result := ExtractFilePath(Paramstr(0)); //This get's the path
end;
{You need to set up on your server a ini file called update.ini containing the following lines}

[version]
wsc=6

then apply this source code:

var
  apath: string;
  new: Integer;
begin
  apath := 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')
    then
  begin
    Gauge1.Progress := 50;
    StatusBAr1.SimplePanel := True;
    StatusBar1.SimpleText := 'Checking for newer versions...';
    vernfo := TiniFile.Create(GetPathPath + '/update.ini');
    new := vernfo.ReadInteger('version', 'wsc', 6);
    vernfo.Free;
    if (old = new) then
    begin
      StatusBar1.SimplePanel := True;
      StatusBar1.SimpleText := 'No new version detected';
      Gauge1.Progress := 100;
    end
    else if DownloadFile('http://www.tsoft.home.ro/winnew.exe', PChar(apath) +
      '/winsafe.exe') then
    begin
      ShowMessage('Update succeseful');
      Gauge1.Progress := 100;
      winsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
      winsc.WriteInteger('wsc', 'vernfo', new);
      winsc.Free;
    end
    else
      MessageDlg('A new version has appeard but it requires a second istall!',
        mtInformation, [mbOk], 0);
  end
  else
  begin
    StatusBAr1.SimplePanel := True;
    StatusBar1.SimpleText := 'Failed probably a internet problem';
    Gauge1.Progress := 0;
  end;
end;
{You will figure it out coz you can't copy the code like this you must make a few adjustments but I provided the ideea}

Nincsenek megjegyzések:

Megjegyzés küldése