ViaThinkSoft CodeLib
Dieser Artikel befindet sich in der Kategorie:
CodeLib → Programmierhilfen → Delphi
uses
ShellAPI;
procedure PerformSoftwareUpdate;
var
sl: TStringList;
const
DOWNLOAD_ZIP = 'software_update.zip';
VBS_SCRIPTNAME = 'software_update.vbs';
begin
CopyFile('d:\test\TEST - Kopie.zip', PChar(IncludeTrailingPathDelimiter((ExtractFilePath(ParamStr(0))))+DOWNLOAD_ZIP), false); // TODO: Die Datei z.B. von einem Webserver herunterladen
OwnParameters := '';
for i := 1 to ParamCount do
begin
OwnParameters := OwnParameters + ParamStr(i) + ' ';
end;
OwnParameters := Trim(OwnParameters);
sl := TStringList.Create;
sl.Add('');
sl.Add(''' Automatische Programmaktualisierung');
sl.Add(''' (C) 2022 Daniel Marschall, ViaThinkSoft');
sl.Add('');
sl.Add(''' ----------------------------------------------------------');
sl.Add(''' Konstanten (werden durch das aufrufende Programm gesetzt)');
sl.Add(''' ----------------------------------------------------------');
sl.Add('');
sl.Add('ZipFile="'+IncludeTrailingPathDelimiter((ExtractFilePath(ParamStr(0))))+DOWNLOAD_ZIP+'"');
sl.Add('ExtractTo="'+IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)))+'"');
sl.Add('StartApp="'+ExtractFileName(ParamStr(0))+'"');
sl.Add('Params="'+OwnParameters+'"');
sl.Add('');
sl.Add(''' ----------------------------------------------------------');
sl.Add(''' Warte etwas (sichergehen, dass das Hauptprogramm geschlossen ist)');
sl.Add(''' ----------------------------------------------------------');
sl.Add('');
sl.Add('WScript.Sleep 1000');
sl.Add('');
sl.Add(''' ----------------------------------------------------------');
sl.Add(''' Nun die Programmdateien entpacken und ggf. überschreiben');
sl.Add(''' ----------------------------------------------------------');
sl.Add('');
sl.Add('Set fso = CreateObject("Scripting.FileSystemObject")');
sl.Add('''If the extraction location does not exist create it.');
sl.Add('If NOT fso.FolderExists(ExtractTo) Then');
sl.Add(' fso.CreateFolder(ExtractTo)');
sl.Add('End If');
sl.Add('''Extract the contants of the zip file.');
sl.Add('set objShell = CreateObject("Shell.Application")');
sl.Add('set FilesInZip=objShell.NameSpace(ZipFile).items');
sl.Add('objShell.NameSpace(ExtractTo).CopyHere FilesInZip, 16 ''16=NoOverwriteConfirmation');
sl.Add('Set fso = Nothing');
sl.Add('Set objShell = Nothing');
sl.Add('');
sl.Add(''' ----------------------------------------------------------');
sl.Add(''' Programm wieder starten');
sl.Add(''' ----------------------------------------------------------');
sl.Add('');
sl.Add('Set objShell = CreateObject("Shell.Application")');
sl.Add('objShell.ShellExecute ExtractTo+StartApp, Params, ExtractTo, "open", 1 ''1=normal');
sl.Add('Set objShell = Nothing');
sl.Add('');
sl.Add(''' ----------------------------------------------------------');
sl.Add(''' Dieses Script und die ZIP-Datei löschen!');
sl.Add(''' ----------------------------------------------------------');
sl.Add('');
sl.Add('Set fso = CreateObject("Scripting.FileSystemObject")');
sl.Add('fso.DeleteFile(ZipFile)');
sl.Add('fso.DeleteFile(WScript.ScriptFullName)');
sl.Add('Set fso = Nothing');
sl.Add('');
sl.SaveToFile(VBS_SCRIPTNAME);
FreeAndNil(sl);
ShellExecute(handle, 'open', VBS_SCRIPTNAME, '', PChar(ExtractFilePath(ParamStr(0))), SW_NORMAL);
Application.Terminate; // Wichtig! Wir haben nur 1 Sekunde Zeit für das Beenden.
end;
Daniel Marschall
ViaThinkSoft Mitbegründer
ViaThinkSoft Mitbegründer