⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoupdate.dpr

📁 全自动更新
💻 DPR
字号:
program PBAutoUpdate;

uses
  Windows,
  Messages,
  SysUtils,
  IdGlobal,
  shellapi;

type
  TFileInfo = record
    FileName: string;
    LastWriteTime: TDateTime;
  end;
  TFileInfoList = array of TFileInfo;
var
  FileListServer, FileListLocal: TFileInfoList;
  ServerFilepath, LocalFilePath: string;
  fileupdate, ShowUpdMSg: boolean;

function CovFileDate(Fd:_FileTime): TDateTime;
// 转换文件的时间格式
var
  Tct: _SystemTime;
  Temp: _FileTime;
begin
  FileTimeToLocalFileTime(Fd,Temp);
  FileTimeToSystemTime(Temp,Tct);
  CovFileDate:=SystemTimeToDateTime(Tct);
end;

procedure GetFileList(
  Path: string;
  var FileList: TFileInfoList
  );
//将指定目录的文件属性放入指定的动态数组中
var
  sr: TSearchRec;
  FileAttrs, FileIndex: Integer;
begin
  FileIndex := 0;
  fileattrs:=faAnyFile;
  if FindFirst(Path, FileAttrs, sr) = 0 then
  begin
    repeat
      if (sr.Attr and FileAttrs) = sr.Attr then
      begin
        setlength(FileList,FileIndex+1);
        FileList[FileIndex].FileName := sr.Name;
        FileList[FileIndex].LastWriteTime := CovFileDate(sr.FindData.ftLastWriteTime);
        inc(FileIndex);
      end;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;
end;

procedure AutoUpdate;
//将服务器上的pbd文件修改时间比客户端新的复制到客户端
var
  i,j: integer;
begin
  for i := low(FileListServer) to high(FileListServer) do
    for j := low(FileListLocal) to high(FileListLocal) do
      if FileListServer[i].FileName = FileListLocal[j].FileName then
      //遍历两动态数组,找到文件名相同的对应文件
        if FileListServer[i].LastWriteTime > FileListLocal[j].LastWriteTime then
        //判断服务器目录下文件的最后修改时间比程序所在目录下的文件新
        begin
         { showmessage(SeverFilepath+FileListServer[i].FileName+
             FormatDateTime('yyyy/mm/dd,hh:mm:ss',FileListServer[i].LastWriteTime)+' '+
             LocalFilepath+FileListLocal[j].FileName+
             FormatDateTime('yyyy/mm/dd,hh:mm:ss',FileListLocal[j].LastWriteTime)
             );  }
          if not ShowUpdMSg then
          begin
            MessageBox(0,'现在的程序文件比较旧,请点击"确定"后等待系统将自动更新这些文件。','',MB_ICONINFORMATION);
            ShowUpdMSg :=true;
          end;
          CopyFile(
            pchar(ServerFilepath + FileListServer[i].FileName),
            pchar(LocalFilepath + FileListLocal[j].FileName),
            false
            );
          fileupdate := true;
        end;
end;

procedure AutoPatch;
var
  i: integer;
begin
  for i := low(FileListServer) to high(FileListServer) do
    if not fileexists(LocalFilepath + FileListServer[i].FileName) then
    //遍历服务器文件列表数组,如在客户端上对应文件不存在,则复制到客户端
    begin
      MessageBox(0,pchar('发现程序文件:' +
        LocalFilepath + FileListServer[i].FileName +
        '缺少,点击"确定", 系统将自动修补这个文件。'),'',MB_ICONINFORMATION);
      CopyFile(
        pchar(ServerFilepath + FileListServer[i].FileName),
        pchar(LocalFilepath + FileListServer[i].FileName),
        false
        );
      fileupdate := true;
    end;
end;

procedure BeginUpdate;
const
  PBDFileCount = 9;//必须的pbd文件数
begin
  //Formmsg := TFormmsg.Create(Formmsg);
  fileupdate := false;
  ShowUpdMSg := false;
  LocalFilePath := ExtractFilePath(ParamStr(0));
  ServerFilePath := ExtractFilePath(ParamStr(1));
  GetFileList(ServerFilepath + '*.pbd', FileListServer);
  GetFileList(LocalFilePath + '*.pbd', FileListLocal);
  if high(FileListServer) = -1 then
  begin
    MessageBox(0,'服务器路径不存在或没有文件,不能保证随时自动升级!','',MB_ICONINFORMATION);
    exit;
  end;
  if high(FileListServer) + 1 < PBDFileCount then
    MessageBox(0,'服务器上程序所需PBD文件数不够,但仍可部份升级!','',MB_ICONINFORMATION);
  AutoUpdate;
  AutoPatch;
  if fileupdate then
    MessageBox(0,'程序已自动升级完成!','',MB_ICONINFORMATION);
  //if MessageBox(0,'是否运行用电系统?','',MB_YESNO + MB_ICONQUESTION) = IDYES then
    //shellexecute(0,'open','power.exe',nil,pchar(LocalFilePath),SW_SHOWDEFAULT);
end;

begin
  BeginUpdate;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -