自解压Delphi程序

Ste*_*eve 6 delphi extraction

我正在Delphi7中编写一个更新程序,它将运行一次,但需要运行许多文件.

我想要的成就:

  1. 用户运行exe
  2. exe解包文件,运行更新程序
  3. 如果updater检测到并出现错误,则提示用户发送登录电子邮件
  4. 运行更新程序后,将删除临时文件(其中一些文件是更新程序使用的dll,因此必须先关闭更新程序才能删除文件)

有谁能推荐一个好的解决方案?我已经考虑过使用Inno Setup(对于这么简单的任务来说太复杂)或使用自解压zip文件(但之后如何删除文件)?

谢谢!

ska*_*adt 12

我个人会使用Inno Setup,因为它非常适合这类任务.使用脚本事件删除文件非常简单,并且在安装后步骤中执行删除操作.


小智 2

我正在使用我的更新程序的资源。我是用 brcc32 编译资源而不是编译更新程序。当更新程序运行其自我检查所有内容并写入或更新新内容时。但关于这个过程,你必须在程序运行的地方正确写入、删除权限。

我在下面添加示例代码。

文件exe.rc

newprog RCDATA Application.exe
Run Code Online (Sandbox Code Playgroud)

文件 makeres.bat

brcc32 exe.rc
Run Code Online (Sandbox Code Playgroud)

文件 updater.dpr

{$R exe.res}
Run Code Online (Sandbox Code Playgroud)

单位文件及程序

procedure ExtractRes(resname,fname,ext:string);
var
 rStream:TResourceStream;
 fStream:TFileStream;
 fNamex:string;
begin
 fNamex:=ExtractFilePath(Application.ExeName)+fname+'.'+ext;
 if not fileExists(fnamex) then
 begin
  try
   rStream:=tresourcestream.Create(hinstance,ResName,RT_rcDATA);
   fStream := TFileStream.Create(fnamex, fmCreate);
   fStream.CopyFrom(rStream, 0);
  finally
   rStream.Free;
   fstream.Free;
  end;
 end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
 apath:string;
begin
 if Timer1.Enabled then
  Timer1.Enabled:=false;
 apath:=ExtractFilePath(Application.ExeName);
 lblMesg.Caption:='Backup old version';
 Application.ProcessMessages;
 if FileExists(apath+'Application_old.bak') then
  DeleteFile(apath+'Application_old.bak') ;
 while FileExists(apath+'Application.exe') do
 begin
  RenameFile(apath + 'Application.exe', apath + 'Application_old.bak');
  sleep(1);
  if FileExists(apath+'Application.exe') then
  begin
   lblMesg.Caption:='It seems application still running. Waiting for end';
   if FileExists(apath+'Application_old.bak') then
    DeleteFile(apath+'Application_old.bak');
   DeleteFile(apath+'Application.exe');
  end;
  Application.ProcessMessages;
 end;
 lblMesg.Caption:='Creating New Version..';
 Application.ProcessMessages;
 ExtractRes('Application','Application','exe');
 lblMesg.Caption:='Running New Version...';
 Application.ProcessMessages;
 WinExec(pchar(apath+'Application.exe'),sw_show);
 Application.ProcessMessages;
 Halt;
end;
Run Code Online (Sandbox Code Playgroud)

我认为这可以帮助你的1,2,3问题。对于 4,您可以扩展代码。