procedure ApplicationUpdate();
var ExeName:String;
    tmpName:String;
    CurrentVersion:string;
    ServerVersion:String;
    FS:TFileStream;
begin
  exeName:='FAQ.exe';
  tmpName:='~FAQ.exe';
  if ExtractFileName(application.ExeName)=exeName then
  begin
    //启动的是旧文件
      if fileexists(extractFilePath(application.ExeName)+tmpName) then deletefile(extractFilePath(application.ExeName)+tmpName)
  end;
  if ExtractFileName(application.ExeName)=tmpName then
  begin
    //启动的是临时文件
    if fileexists(extractFilePath(application.ExeName)+exeName) then deletefile(extractFilePath(application.ExeName)+exeName);
    copyfile(pansichar(extractFilePath(application.ExeName)+tmpName),pansichar(extractFilePath(application.ExeName)+exeName),true);
    shellexecute(application.Handle,'open',pansichar(extractFilePath(application.ExeName)+exeName),nil,nil,SW_SHOW);
    application.Terminate;
  end;
  with dm.Adoqry_temp do
  begin
      //查询服务端的版本号信息
      close;
      sql.Text:='select version from product where id in (select max(id) from product)';
      open;
      if RecordCount=0 then exit;
      CurrentVersion:=FileVersion(application.ExeName);
      ServerVersion:=fieldbyname('version').Value;
      close;
      //如果版本不一致,就开始下载文件
      if CurrentVersion<>ServerVersion  then
      begin
        if application.MessageBox(Pansichar('此程序版本:'+currentVersion+#13+'服务器版本:'+serverVersion+#13+'点击"确定"进行更新'),'系统检测到新版本',MB_OKCANCEL)=ID_CANCEL then exit;
        dm.adoqry_temp.SQL.Text:='select [file] from product where id in (select max(id) from product)';         dm.Adoqry_temp.Open;
        FS:=TFileStream.Create(ExtractFilePath(application.ExeName)+tmpName,fmCreate);
        LoadFromImage(dm.Adoqry_temp.FieldByName('file'),FS);
        FS.Free;
        dm.adoqry_temp.close;
        //下载完后开始运行新文件并关闭本程序
        shellexecute(application.Handle,'open',pansichar(extractFilePath(application.ExeName)+tmpName),nil,nil,SW_HIDE);
        application.terminate;
      end;
  end;
end;

解决方案 »

  1.   

    FAQ.exe为当前应用程序,~FAQ.exe为新版本的程序,然后通过这段让它实现自我更新
    ~FAQ.exe可通过从数据库中下载或通过文件共享的方式来获得,具体采用哪种方式就是你们自己的事喽。有什么问题可以提出来,有什么不明白直接问我,我懒得写注释。也许代码不够优化。呵呵
      

  2.   

    前提是新旧两个程序必须包含这段代码才行。。Hoho
    反正我就是通过种方式来实现的自我更新,不需借用第三个程序来处理
      

  3.   

    其中FileVersion过程和LoadFromImage是我自己写的一个过程,用来获取文件版本用或从SqlServer获取新升级程序用的。开发模式是用CS模式的
      

  4.   

    sql.Text:='select version from product where id in (select max(id) from product)';
    客户端嵌入SQL,偶一直认为是相当的不妥……
    其他的不发表意见
      

  5.   


    如果不嵌入SQL的话,要怎么处理,我说过是用CS模式的嘛,既然是单个文件自我更新,有必要搞那么麻烦吗?
      

  6.   

    CS模式这样做应该是个不错的办法。。要是bs+cs的话,更新这块就简单多了
      

  7.   

    服务器开放web服务,做个配置文件去读取貌似比客户端嵌入sql安全点。
    个人看法,仅供参考