class procedure TVTUpgrader.UpdateSelf(const FileName: TVTString);
var
  LBatchFile: TextFile;
  LBatchFileName: TVTString;
  LProcessInfo: TProcessInformation;
  LStartUpInfo: TStartupInfo;
begin
  //批处理文件名
  LBatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';

  //打开文件,设置覆盖模式
  AssignFile(LBatchFile, LBatchFileName);
  Rewrite(LBatchFile);

  //写入批处理内容
  Writeln(LBatchFile, ':try');
  Writeln(LBatchFile, 'del "' + ParamStr(0) + '"');
  Writeln(LBatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try');
  Writeln(LBatchFile, 'copy "' + FileName + '" "' + ParamStr(0) + '"');
  Writeln(LBatchFile, 'del "' + FileName + '"');
  Writeln(LBatchFile, 'del %0');

  //关闭文件
  CloseFile(LBatchFile);

  //创建新进程运行批处理文件
  FillChar(LStartUpInfo, SizeOf(LStartUpInfo), $00);
  LStartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
  LStartUpInfo.wShowWindow := SW_HIDE;
  if CreateProcess(nil, PChar(LBatchFileName), nil, nil, False,
    IDLE_PRIORITY_CLASS, nil, nil, LStartUpInfo, LProcessInfo) then
  begin
    CloseHandle(LProcessInfo.hThread);
    CloseHandle(LProcessInfo.hProcess);
  end;
end;

相关文章:

  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2021-05-29
  • 2021-09-05
猜你喜欢
  • 2021-12-19
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案