首页 > 代码库 > delphi 绿色exe文件自我删除和更新

delphi 绿色exe文件自我删除和更新

发布绿色exe文件后,再更新就需要把自己删除,用更新文件代替。  当然还需要其他细节比如隐藏老版本文件,还原老版本文件等 ;

点击更新按钮后,先对自己重命名

procedure TFrmCheckUpdate.ActUpdateNowExecute(Sender: TObject); // 现在升级
begin
  {$IFDEF MSWINDOWS}
 // RenameFile(Paramstr(0),FormatDateTime(‘yyyymmddhhnn‘,Now)+‘Newname.exe‘); //老版本文件重命名
  if not SaveDialog1.Execute then Exit;
  AppFileName := SaveDialog1.FileName;

  {$ENDIF}

  TThread.CreateAnonymousThread(
    procedure
    begin
      {$IFDEF MSWINDOWS}
      SetFormState(TFormState.fsDownloading);
      HttpDownload(IdHTTP, AppURL, AppFileName, False);
      {$ENDIF}
    end).Start;

end;

在别人家找到一个自己我删除的代码,亲测有效;

procedure TFrmCheckUpdate.DeleteMe;
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ChangeFileExt(Paramstr(0),‘.bat‘);
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
// build cmd batch file
Writeln(BatchFile, ‘:try‘);
Writeln(BatchFile, Format(‘del "%s"‘, [ParamStr(0)]));
Writeln(BatchFile, Format(‘if exist "%s" goto try‘, [ParamStr(0)]));//循环删除
Writeln(BatchFile, ‘del %0‘);
CloseFile(BatchFile);
FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_HIDE;
// create hidden process
if CreateProcess(nil,PChar
(BatchFileName),nil,nil,False,IDLE_PRIORITY_CLASS,nil,nil,StartUpInfo,ProcessInfo) then// 创建线程 
     begin
       CloseHandle(ProcessInfo.hThread);
       CloseHandle(ProcessInfo.hProcess);
     end;
end;

delphi 绿色exe文件自我删除和更新