this slowpoke moves

Update Program via Internet

uses ExtCtrls, IniFiles, UrlMon, Gauges, ComCtrls

private
    { Private declarations }
    winsc: TiniFile;
    old: Integer;
    vernfo: TIniFile;
    
//

function DownloadFile(Source, Dest: string): Boolean;
  { Function for Downloading the file found on the net }
begin
  try
    Result := UrlDownloadToFile(nil, PChar(Source), PChar(Dest), 0, nil) = 0;
  except
    Result := False;
  end;
end;

function GetPathPath: string;
  { Retrive app path }
begin
  Result := ExtractFilePath(Application.ExeName);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //App version
  winsc := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    old := winsc.ReadInteger('wsc', 'vernfo', 1);
  finally
    winsc.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  apath: string;
  new: Integer;
begin
  // This is the exact code from my application
  apath           := GetPathPath;
  Form1.Gauge1.Progress := 0;
  Form1.StatusBar1.SimplePanel := True;
  Form1.StatusBar1.SimpleText := 'Connecting to https://www.website.de';
  Form1.Gauge1.Progress := 20;

  if DownloadFile('https://www.website.de/update.ini', PChar(apath) + '/update.ini') then
  begin
    Form1.Gauge1.Progress := 50;
    Form1.StatusBAr1.SimplePanel := True;
    Form1.StatusBar1.SimpleText := 'Checking for newer versions...';
    Form1.vernfo := TiniFile.Create(GetPathPath + '/update.ini');
    new    := Form1.vernfo.ReadInteger('version', 'wsc', 7);
    Form1.vernfo.Free;

    if (Form1.old = new) then
    begin
      Form1.StatusBar1.SimplePanel := True;
      Form1.StatusBar1.SimpleText  := 'No new version detected';
      Form1.Gauge1.Progress        := 100;
    end
    else if DownloadFile('https://www.website.de/myExe.exe',
      PChar(apath) + '/myExe.exe') then
    begin
      ShowMessage('Update succeseful');
      Form1.Gauge1.Progress := 100;
      Form1.winsc           := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
      Form1.winsc.WriteInteger('wsc', 'vernfo', new);
      Form1.winsc.Free;
    end
    else
      MessageDlg('A new version has appeard but it requires a second install',
        mtInformation, [mbOK], 0);
  end
  else
  begin
    Form1.StatusBar1.SimplePanel := True;
    Form1.StatusBar1.SimpleText  := 'Failed to connect probably a internet problem';
    Form1.Gauge1.Progress        := 0;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate