目的:系统启动时自动检测服务器是否有升级版本,如有自动将文件下载到系统目录下,在下载文件名后加.bak,之后系统自动关闭,在OnFormClose有以下代码:procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
  F:TextFile;
begin
  if isUpdate then
  begin
    Action := caFree;
    AssignFile(F,'delself.bat');
    Rewrite(F);{F为TextFile类型}
    WriteLn(F,'del ' + ExtractFileName(Application.ExeName));  //删除执行文件  
    WriteLn(F,'ren ' + ExtractFileName(Application.ExeName) + '.bak ' +
      ExtractFileName(Application.ExeName));  //将下载的文件改名为执行文件
    WriteLn(F,'del %0');  //删除Bat文件
    CloseFile(F);
    WinExec('delself.bat',SW_HIDE);   //执行批次文件
    isUpdate := False;
  end else
  begin
    if ShowAppMessage(isClose,4 + 32) = 7 then
      Action := caNone
    else
      Action := caFree;
  end;
end;问题:
  假如不用更改文件名那行,能够自杀。有这行之后,不但不能改名而且程式都不能自杀;
说明:在Windows下直接运行delself.bat能够达到以上目的。

解决方案 »

  1.   

    看了一下,你把那句Action := caFree;改成PostMessage(self.handle, WM_CLOSE, 0, 0);试试
    我通过了哦.
      

  2.   

    这样即可:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      UpdateMe('AutoUpdate.EX_');
    end;
    procedure TForm1.UpdateMe(UpdateFileName:string); //程序自杀并升级
    var
      BatchFile: TextFile;
      BatchFileName: string;
      ProcessInfo: TProcessInformation;
      StartUpInfo: TStartupInfo;
    begin
    {By Lovejingtao.http://Lovejingtao.126.com,[email protected]}
      BatchFileName := ExtractFilePath(ParamStr(0)) + '$$a.bat';
      AssignFile(BatchFile, BatchFileName);
      Rewrite(BatchFile);
      Writeln(BatchFile, ':try');
      Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
      Writeln(BatchFile,
        'if exist "' +
        ParamStr(0) +
        '"' +
        ' goto try');
      Writeln(BatchFile,'copy "'+UpdateFileName+ '" "' +ParamStr(0) +'"');
      Writeln(BatchFile, 'del "' + BatchFileName + '"');
      Writeln(BatchFile, 'cls');
      CloseFile(BatchFile);
      FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
      StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartUpInfo.wShowWindow := SW_HIDE;
      if CreateProcess(
        nil,
        PChar(BatchFileName),
        nil,
        nil,
        False,
        IDLE_PRIORITY_CLASS,
        nil,
        nil,
        StartUpInfo,
        ProcessInfo
        ) then
      begin
        CloseHandle(ProcessInfo.hThread);
        CloseHandle(ProcessInfo.hProcess);
      end;
      Application.Terminate;end;