Einzelnen Beitrag anzeigen

Benutzerbild von SaFu
SaFu

Registriert seit: 8. Nov 2006
1.360 Beiträge
 
Delphi 10.3 Rio
 
#47

Re: Einfaches Online Update

  Alt 14. Jul 2008, 08:32
So ich habe Poste mal mein Update, ich habe es auf der Basis von Larsi aufgebaut.

Delphi-Quellcode:
  private
  bUpdateNochOffen : Boolean;
  function IsInternetConectet: Boolean; // Internetverbindung überprüfen
  procedure DownloaddateiPruefen;

  public
  INIEinstellungenProgramm,IniProgrammeinstellung :TFileName;
  
  {Constanten für Ini.}
  const DownloadInfo = 'DownloadInfo';
        BtnUpdatInstall = 'Installation steht noch aus';
        BtnUpdateueberp = 'Updateüberprüfung';
        BtnDownload = 'Downloaden';

        ProgrammInfo = 'Programminfo';
        Programmversion = 'Programmversion';

  end;

var
  UpdateForm: TUpdateForm;

implementation


{$R *.dfm}

{ TUpdateForm }

//=====Einstellungen laden=============================
procedure TUpdateForm.FormShow(Sender: TObject);
begin
  Edit5.Text:= Form1.GetFileVersion(ParamStr(0)); // Programversion auslesen

  if not FileExists(ExtractFilePath(ParamStr(0))+ 'Einstellungen\ProgrammSettings.ini') then
   begin
    IniProgrammeinstellung:= (ExtractFilePath(ParamStr(0))+ 'Einstellungen\ProgrammSettings.ini');
     begin
      with TMemIniFile.Create(IniProgrammeinstellung ) do
      try
       begin
        WriteString(Programminfo,Programmversion,'0');
        WriteBool(DownloadInfo,BtnUpdateueberp,True);
        WriteBool(DownloadInfo,BtnDownload,False);
        WriteBool(DownloadInfo,BtnUpdatInstall,False);
        UpdateFile;
       end;
      finally
       Free;
      end;
     end;
   end;
   
  // Lokale Einstellungen für Programm
  IniProgrammeinstellung:= (ExtractFilePath(ParamStr(0))+ 'Einstellungen\ProgrammSettings.ini');
   with TMemIniFile.Create(IniProgrammeinstellung) do
    try
     Label11.Caption:= ReadString(Programminfo,Programmversion,Label11.Caption);
     BtnUpdateUeberpruefung.Enabled:= ReadBool(DownloadInfo,BtnUpdateueberp,BtnUpdateUeberpruefung.Enabled);
     BtnUpdateDownloaden.Enabled:= ReadBool(DownloadInfo,BtnDownload,BtnUpdateDownloaden.Enabled);
     BtnUpdateInstallieren.Enabled:= ReadBool(DownloadInfo,BtnUpdatInstall,BtnUpdateInstallieren.Enabled);
     bUpdateNochOffen:= ReadBool(DownloadInfo,BtnUpdatInstall,bUpdateNochOffen);
    finally
     Free;
    end;


  if bUpdateNochOffen = True then // wenn das Installieren des Updates noch aussteht
   begin
    Label15.Visible:= True;
    Label15.Font.Color:= ClRed;
    Label15.Caption:= ('Update Installation steht noch aus!!!');
   end
  else
  begin
   Label15.Visible:= False;
  end;
end;


//=========Updateüberprüfung starten==================
procedure TUpdateForm.Button2Click(Sender: TObject);
var DownloadDatei,DownloadZiel: PChar;
begin
 try
   if IsInternetConectet = True then // Internet Verbindung da
    begin
     ProgressBar1.Position:= 0;
     Label6.Font.Color:= ClLime;
     Label6.Caption:= 'Überprüfung gestartet';
      try
       DownloadDatei:= 'http://Techniker-werden.de/update/UpdateDatei.txt'; // Von Internetseite Versions Info
       DownloadZiel:= PChar(ExtractFilePath(ParamStr(0))+ 'Einstellungen\System\UpdateDatei.txt'); // Ins Zielverzeichnis
      
       UrlDownloadToFile(nil, DownloadDatei, DownloadZiel, 0, nil); Von --> Nach Downloaden
       
       Label6.Font.Color:= ClLime;
       Label6.Caption:= 'Überprüfung beendet';

      except
       Label6.Font.Color:= ClRed;
       Label6.Caption:= ('Überprüfung abgebrochen');
      end;
     DownloadDateiPruefen;
    end
   else
   begin
    Form1.MessageDlgPos2('Es besteht keine Verbindung zum Internet',mtInformation,[mbOk],self);
    BtnUpdateInstallieren.Enabled:= False;
    Edit5.Text:= Form1.GetFileVersion(ParamStr(0));// versionsnummer anzeigen
   end;
  except
   Form1.MessageDlgPos2('Unbekannter Fehler',mtInformation,[mbOk],self);
 end;
end;

//========exe updaten==========================================================
procedure TUpdateForm.Button3Click(Sender: TObject);
var DownloadEXE,DownloadEXEZiel: PChar;
begin
 TimerLabelBlinken.Enabled:= False; // Blinken von Label wenn Update vorhanden

 Label7.Visible:= true;
 Label7.Font.Color:= ClLime;
 Label7.Caption:= ('Download wird gestartet bitte warten');

 Label12.Font.Color:= ClLime;
 Label12.Caption:= 'Download wird gestartet bitte warten';

  try
    DownloadEXE:= 'http://Techniker-werden.de/update/Programm.exe'; // Programm Downloaden
    DownloadEXEZiel:= PChar(ExtractFilePath(ParamStr(0))+ 'Einstellungen\System\Programm.exe'); //ns Zielverzeichnis
   
    UrlDownloadToFile(nil, DownloadEXE, DownloadEXEZiel, 0, nil); Von --> Nach Downloaden
   
    TimerProgressbar.Enabled:= True;
  except
    Label12.Font.Color:= ClRed;
    Label12.Caption:= ('Download abgebrochen');
  end;

  BtnUpdateDownloaden.Enabled:= False; // Button Downloaden

 IniProgrammeinstellung:= (ExtractFilePath(ParamStr(0))+ 'Einstellungen\ProgrammSettings.ini');
  begin
   with TMemIniFile.Create(IniProgrammeinstellung ) do
    try
      begin
       WriteBool(DownloadInfo,BtnUpdatInstall,True);
       WriteBool(DownloadInfo,BtnUpdateueberp ,False);
       WriteBool(DownloadInfo,BtnDownload,False);
       UpdateFile;
      end;
      finally
     Free;
    end;
  end;
  Label6.Font.Color:= ClBlack;
  Label6.Caption:= 'Nicht aktiv';
end;


//==========Textdatei auslesen mit Versionsinfo auslesen================================
procedure TUpdateForm.DownloaddateiPruefen;
Var List: TStrings;
begin
 List:= TStringList.Create;
 try
  List.LoadFromFile(ExtractFilePath(ParamStr(0))+ 'Einstellungen\System\UpdateDatei.txt');
  if List.Count>0 then
    Edit1.Text:=List[0];
    Edit2.Text:=List[1];
    Edit3.Text:=List[2];
    Edit4.Text:=List[3];
 finally
  List.Free;
 end;

 //Edit1 und Label11 sind Visble:= False dient nur im Hintergrund zur Überprüfung
 if StrToInt(Edit1.text) > StrToInt(Label11.Caption) then
  begin
   TimerProgressbar.Enabled:= True;
   Label7.Font.Color:= ClLime;
   Label7.Caption:= ('Es ist ein neues Update vorhanden!');
   BtnUpbdateDownloaden.Enabled:= True;
  end
 else
  begin
   Label7.Font.Color:= ClRed;
   Label7.Caption:= ('Sie haben das aktuellste Update!');
   BtnUpdateDownloaden.Enabled:= False;
  end;
end;


//=========download Progrssbar=================================================
procedure TUpdateForm.TimerProgressbarTimer(Sender: TObject);
begin
 Label7.Font.Color:= ClLime;
 Label7.Caption:= ('Update Download gestartet');

 Label12.Font.Color:= ClLime;
 Label12.Caption:= 'Update Download gestartet';
  ProgressBar1.Position := ProgressBar1.Position + 33;
  if ProgressBar1.Position = 99 then
    begin
      ProgressBar1.Position := 100;

      Label7.Font.Color:= ClLime;
      Label7.Caption:= ('Download erfolgreich beendet');

      Label12.Font.Color:= ClLime;
      Label12.Caption:= 'Download erfolgreich beendet';
      TimerProgressbar.Enabled := False;
      BtnUpdateInstallieren.Enabled:= True;
    end;

 IniProgrammeinstellung:= (ExtractFilePath(ParamStr(0))+ 'Einstellungen\ProgrammSettings.ini');
  begin
    with TMemIniFile.Create(IniProgrammeinstellung ) do
    try
      begin
       WriteString(ProgrammInfo,Programmversion,Edit1.Text);
       UpdateFile;
      end;
      finally
     Free;
    end;
  end;
end;

//===============Updater aufrufen===============================================
procedure TUpdateForm.Button4Click(Sender: TObject);
begin
 if Form1.MessageDlgPos2('Um das Update zu installieren wird das Programm beendet',mtInformation, [mbNo,mbYes],self) = mrYes then
  begin
    BtnUpdateInstallieren.Enabled:= False; //Button Installieren
    
    // Updater aufrufen
    if ShellExecute(Application.Handle,'open',PChar(ExtractFilePath(ParamStr(0)) +'Updater.exe'),Nil, Nil,SW_NORMAL) >= 32 then

    MainForm.Close; // Programm Schließen

    Label12.Caption:= ('Status');
    Progressbar1.Position:= 0;
  end
 else
  begin

  end;
end;

//===========für Blinken vom Label========================
procedure TUpdateForm.Timer2Timer(Sender: TObject);
begin
 Label7.Visible:= Not Label7.Visible;
 BtnUpdateDownload.Enabled:= true;
end;

//Internetverbindung Überprüfen============================
function TUpdateForm.IsInternetConectet: Boolean;
begin
if InternetGetConnectedState(nil,0) then
 begin
   Result:= True;
 end
else
 begin
   Result:= False;
 end;
end;
so bis hier hin war das die Updateüberprüfung und das Downloaden

Jetzt Kommt das Kopieren und Ersetzen des Programms, dazu wurde vorher das Hauptrogramm geschlossen und ein anderes gestartet das sich im Programmverzeichnis befindet.

Delphi-Quellcode:
private
  IniProgrammeinstellung :TFileName;
  Datei :Textfile;

public
  ServerPfad :String;
 
 Const
   ProgrammInfo = 'Programminfo';
   Programmversion = 'Programmversion';

   DownloadInfo = 'DownloadInfo';
   BtnUpdatInstall = 'Installation steht noch aus';
   BtnUpdateueberp = 'Updateüberprüfung';
   BtnDownload = 'Downloaden'; { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//====Programm löschen und Timer für Kopie starten========
procedure TForm1.Button1Click(Sender: TObject);
begin
 DeleteFile(ExtractFilePath(ParamStr(0)) + 'Programm.exe');
 TimerUpdaten.Enabled:= True;
end;

//==Progessbar und Kopieren starten========================
procedure TForm1.TimerUpdatenTimer(Sender: TObject);
begin
 TimerKopieren.Enabled:= True; // Kopieren starten

  ProgressBar1.Position:= ProgressBar1.Position + 33;
  if ProgressBar1.Position = 99 then
    begin
      ProgressBar1.Position:= 100;
      TimerUpdaten.Enabled:= False;
    end;
end;

procedure TForm1.TimerKopierenTimer(Sender: TObject);
var NeuesUpdate,AltesUpdate :string;
     List: TStrings;
begin
  NeuesUpdate:= (ExtractFilePath(ParamStr(0)) + 'Einstellungen\System\Programm.exe');
  AltesUpdate:= (ExtractFilePath(ParamStr(0)) + 'Programm.exe');
  begin
   CopyFile(PChar(NeuesUpdate), PChar(AltesUpdate), true);
   begin
    List:= TStringList.Create;
    try
     List.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'Einstellungen\System\UpdateDatei.txt');
      if List.Count > 0 then
       Edit1.Text:= List[0];
     finally
     List.Free;
    end;

    IniProgrammeinstellung:= (ExtractFilePath(ParamStr(0)) + 'Einstellungen\ProgrammSettings.ini');
     begin
      with TMemIniFile.Create(IniProgrammeinstellung) do
      try
        begin
         WriteString(ProgrammInfo,Programmversion,Edit1.Text);
         WriteBool(DownloadInfo,BtnUpdatInstall,False);
         WriteBool(DownloadInfo,BtnUpdateueberp ,True);
         WriteBool(DownloadInfo,BtnDownload,False);
         UpdateFile;
        end;
        finally
       Free;
      end;
     end;
    TimerUpdaten.Enabled:= False;
    MessageDlg('Update erfolgreich durchgeführt, Sie können das Programm jetzt wieder starten', mtInformation, [mbOK], 0);
   Close;
   end;
  end;
end;
So das war das ganze Updaten hoffe man sieht das es sehr einfach ist.
Im Anhang noch mal die Oberfläsche der Updateüberpüfung

Ich weis das mit der Progressbar könnte man noch schöner machen.

Gruß sascha
Miniaturansicht angehängter Grafiken
update_berp_fung_171.jpg  
"... Wie gesagt, die Nutzer von 10, 12, 14 Zoll Display's werden deine Seite nicht in voller Pracht sehen können, ...

Surft Ihr mit dem Taschenrechner?"
  Mit Zitat antworten Zitat