procedure TFrmMain.btnUpdateFilesClick(Sender: TObject);
begin
  try
    if Assigned(gEXEmm) then
      FreeAndNil(gEXEmm);
    if Assigned(gINImm) then
      FreeAndNil(gINImm);

gEXEmm := TFileStream.Create(ExtractFilePath(Application.ExeName) +
      'download\jlpos.exe', fmOpenRead);
    gINImm := TFileStream.Create(ExtractFilePath(Application.ExeName) +
      'download\client.ini', fmOpenRead);

gEXEmm2.Clear;
    gINImm2.Clear;
    gEXEmm2.CopyFrom(gEXEmm, 0);
    gINImm2.CopyFrom(gINImm, 0);

FreeAndNil(gEXEmm);
    FreeAndNil(gINImm);
  except
    on E: Exception do
    begin
      gSysLog.WriteLog('btnUpdateFilesClick: ' + E.Message);
      Exit;
    end;
  end;
end;

function TServerMethods1.DownLoadFile(const FileName: string): TStream;
var
  f: string;
begin
  Result := nil;
  try
    f := ExtractFilePath(Application.ExeName) + 'download\' + FileName;
    if not FileExists(f) then
      Exit;
    Result := TMemoryStream.Create;
    if LowerCase(FileName) = 'jlpos.exe' then
      Result.CopyFrom(gEXEmm2, 0)
    else if LowerCase(FileName) = 'client.ini' then
      Result.CopyFrom(gINImm2, 0);
    Result.Position := 0;
  except
    on E: Exception do
    begin
      gSysLog.WriteLog('DownLoadFile: ' + E.Message);
      Exit;
    end;
  end;
end;

function TdmCommonFun.DownLoadFile(const FileName: string): Boolean;
var
  a: TServerMethods1Client;
  ini: TIniFile;
  Stream, ms: TStream;
  Buffer: TBytes;
  ReadCount: Integer;
const
  BufSize = $F000;
begin
  Result := False;
  if (not TryConnectAPPServer) or (FileName = '') then
    Exit;
  a := TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  ms := TMemoryStream.Create;
  try
    Stream := a.DownLoadFile(FileName);
    if Stream.Size = -1 then
    begin
      SetLength(Buffer, BufSize);
      repeat
        ReadCount := Stream.Read(Buffer[0], BufSize);
        if ReadCount > 0 then
          ms.WriteBuffer(Buffer[0], ReadCount);
        if ReadCount < BufSize then
          break;
      until ReadCount < BufSize;
    end
    else
    begin
      ms.CopyFrom(Stream, 0);
    end;
    // delete bak files
    if FileExists(ExtractFilePath(Application.ExeName) + FileName + 'bak') then
      DeleteFile(PWideChar(ExtractFilePath(Application.ExeName) + FileName
        + 'bak'));
    // 现有文件改名
    if FileExists(ExtractFilePath(Application.ExeName) + FileName) then
    begin
      RenameFile(ExtractFilePath(Application.ExeName) + FileName,
        ExtractFilePath(Application.ExeName) + FileName + 'bak');
    end;
    // 下载最新文件
    TMemoryStream(ms).SaveToFile(ExtractFilePath(Application.ExeName) +
      FileName);
    // 更新本机版本号
    ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'client.ini');
    try
      ini.WriteInteger(FileName, 'ver', GetVer(FileName));
    finally
      ini.Free;
    end;
  finally
    a.Free;
    ms.Free;
  end;
  Result := True;
end;

转载于:https://www.cnblogs.com/hnxxcxg/archive/2012/12/29/2839358.html

最新文章

  1. C#/ASP.NET完善的DBHelper,配套Model生成器
  2. LeetCode题解-----Wildcard Matching
  3. C#基础系列——一场风花雪月的邂逅:接口和抽象类
  4. 在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器
  5. Requirements of children
  6. 24小时学通Linux内核--内核探索工具类
  7. linux两台server远程copy文件
  8. 关于document.write()重写页面
  9. 10676 涂鸦跳跃(sort)
  10. 基于Ubuntu14.04-LTS下安装docker
  11. 【Unity优化】构建一个拒绝GC的List
  12. 漫画|Linux 并发、竞态、互斥锁、自旋锁、信号量都是什么鬼?(转)
  13. MT【254】值域包含值域
  14. mysql数据库优化(二)
  15. bat 批处理脚本定时执行命令
  16. Redis多API开发实践
  17. WindowsPhone自定义控件详解(二) - 模板类库分析
  18. react书写规范小记
  19. node - 上传文件并且修改名称
  20. Hive中集合类型

热门文章

  1. 新式类__new__()方法
  2. python全栈测试之路-py3基础:基础的常用语法知识
  3. harbor清理存储
  4. 查看创世区块 Genesis Block和channel.tx文件
  5. CTF基础知识 &amp;&amp; AWD红蓝对抗
  6. jQuery UI Widget(1.8.1)工作原理
  7. springboot datajpa 简明说明
  8. js中函数总结(1)
  9. SQLite进阶-17.视图
  10. Python 第二式