自动监测当前软件的版本,如果有更高版本则进行在线升级,如何实现呢?

解决方案 »

  1.   

    用pb作过,目前仍在运行中,delphi我还没作过,转delphi才2个多月,不过基本方法就是先判断升级文件的修改日期大小,如果需要升级,我用的ftp传输,中间调用了winrar压缩解压缩,ftp我用了mftpx组件,你可以参考一下。其实核心代码就几行,大部分代码都用来控制界面了,delphi中ftp我还没用呢,用什么控件比较好,尤其错误处理方面,是不是indy?
      

  2.   

    需要做一个selfupdate.exe的程序,其实就是一个ftp功能就够了
      

  3.   

    这是一个检查版本更新的unit update_f;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, TFlatGroupBoxUnit, TFlatButtonUnit, StdCtrls,
      TFlatSpeedButtonUnit, TFlatEditUnit, TFlatMemoUnit, Psock, NMHttp,ShellAPI;type
      TSimpleVersion=record
        dwProductVersionMS: DWORD;
        dwProductVersionLS: DWORD;
      end;
      TUpdate=record      { Structure of update information }
        Name:String[63];
        Version:TSimpleVersion;
        Date:TDate;
        URL1:ShortString;
        URL2:ShortString;
        Update_Info:String;
      end;
      Tupdate_v = class(TForm)
        CurrentVer_GroupBox: TFlatGroupBox;
        NewVer_GroupBox: TFlatGroupBox;
        Update_Btn: TFlatButton;
        Exit_Btn: TFlatButton;
        CurrentVerL_Label: TLabel;
        CurrentDateL_Label: TLabel;
        NewVerL_Label: TLabel;
        NewDateL_Label: TLabel;
        DownAddress1_Label: TLabel;
        UpdateInfo_Memo: TFlatMemo;
        DownAddress1_Edit: TFlatEdit;
        DownAddress1_Btn: TFlatSpeedButton;
        Update_NMHTTP: TNMHTTP;
        NewVer_Label: TLabel;
        NewDate_Label: TLabel;
        Info_Memo: TFlatMemo;
        CurrentVer_Label: TLabel;
        CurrentDate_Label: TLabel;
        DownAddress2_Label: TLabel;
        DownAddress2_Btn: TFlatSpeedButton;
        DownAddress2_Edit: TFlatEdit;
        procedure Exit_BtnClick(Sender: TObject);
        procedure Update_NMHTTPConnect(Sender: TObject);
        procedure Update_NMHTTPConnectionFailed(Sender: TObject);
        procedure Update_NMHTTPDisconnect(Sender: TObject);
        procedure Update_NMHTTPHostResolved(Sender: TComponent);
        procedure Update_NMHTTPStatus(Sender: TComponent; Status: String);
        procedure Update_BtnClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure DownAddress1_BtnClick(Sender: TObject);
        procedure DownAddress2_BtnClick(Sender: TObject);
      private
        { Private declarations }
        Update_New, Update_Ori: TUpdate;
        function AnalyseUpdate(Body: String; var Update: TUpdate): Boolean;
        procedure DisplaySuggestion(b: Boolean);
      public
        { Public declarations }
      end;var
      update_v: Tupdate_v;implementation
    const update_text='http://boyzxd.myrice.com/update.txt';
    {$R *.dfm}//取得当前使用版本的信息
    function GetBuildInfo(FName:string):TSimpleVersion;
    var
      VerInfoSize: DWORD;
      VerInfo: Pointer;
      VerValueSize: DWORD;
      VerValue: PVSFixedFileInfo;
      Dummy: DWORD;
    begin
      VerInfoSize := GetFileVersionInfoSize(PChar(FName), Dummy);
      GetMem(VerInfo, VerInfoSize);
      GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
      VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
      with VerValue^ do
      begin
        Result.dwProductVersionMS := dwFileVersionMS;
        Result.dwProductVersionLS := dwFileVersionLS;
      end;
      FreeMem(VerInfo, VerInfoSize);
    end;function SeparateVerStr(s:String):TSimpleVersion;
    const
      Separator = '.';
    var
      p:WORD;
      v1,v2,v3,v4:WORD;
    begin
      if Length(s)=0 then Exit;  p:=pos(Separator, s);
      v1:=StrToInt(copy(s,1,p-1));
      Delete(s,1,p);
      p:=Pos(Separator,s);
      v2:=StrToInt(copy(s,1,p-1));
      Delete(s,1,p);
      p:=Pos(Separator,s);
      v3:=StrToInt(copy(s,1,p-1));
      Delete(s,1,p);
      v4:=StrToInt(s);
      Result.dwProductVersionMS:=v1*$10000+v2;
      Result.dwProductVersionLS:=v3*$10000+v4;
    end;
      

  4.   

    function Tupdate_v.AnalyseUpdate(Body:String;var Update:TUpdate):Boolean;
    var
      TmpStr,Ver:String;
      function CenterStr(Src:String;Before,After:String):String;
      var
        Pos1,Pos2:WORD;
      begin
        Pos1:=Pos(Before,Src)+Length(Before);
        Pos2:=Pos(After,Src);
        Result:=Copy(Src,Pos1,Pos2-Pos1);
      end;
    begin
      TmpStr:=CenterStr(Body,Format('[%s]',[LowerCase(Update_Ori.Name)]),Format('[/%s]',[LowerCase(Update_Ori.Name)]));
      if TmpStr='' then Result:=False
      else
      begin
        Ver:=CenterStr(TmpStr,'<ver>','</ver>');
        Update.Version:=SeparateVerStr(Ver);
        Update.Date:=StrToDate(CenterStr(TmpStr,'<date>','</date>'));
        Update.URL1:=CenterStr(TmpStr,'<url1>','</url1>');
        Update.URL2:=CenterStr(TmpStr,'<url2>','</url2>');
        Update.Update_Info:=CenterStr(TmpStr,'<info>','</info>');
        Result:=True;
        Info_Memo.Lines.Add('最新版本:'+Ver);
        Info_Memo.Lines.Add('发布日期:'+DateToStr(Update.Date));
        Info_Memo.Lines.Add('下载地址1:'+Update.URL1);
        Info_Memo.Lines.Add('下载地址2:'+Update.URL2);
      end;
    end;{ Compare tow versions whether need update, True stand for need }
    function VersionCheck(OriVer,NewVer:TSimpleVersion):Boolean;
    begin
      if (OriVer.dwProductVersionMS=NewVer.dwProductVersionMS) then
        Result:=OriVer.dwProductVersionLS<NewVer.dwProductVersionLS
      else Result:=OriVer.dwProductVersionMS<NewVer.dwProductVersionMS;
    end;procedure Tupdate_v.DisplaySuggestion(b:Boolean);
    begin
      Info_Memo.Lines.Add('');
      Info_Memo.Lines.Add('青苔提示:');
      if b then
      begin
        if VersionCheck(Update_Ori.Version,Update_New.Version) then
          Info_Memo.Lines.Add('找到一个更新的版本')
       else
          Info_Memo.Lines.Add('对不起,青苔还未发布更新版本');
      end else
      begin
        Info_Memo.Lines.Add('查找版本更新情况失败,');
        Info_Memo.Lines.Add('请检查该版本是否正常,');
        Info_Memo.Lines.Add('或系统是否连上了Internet。');
      end;
    end;procedure Tupdate_v.Exit_BtnClick(Sender: TObject);
    begin
      Close;
    end;procedure Tupdate_v.Update_NMHTTPConnect(Sender: TObject);
    begin
      Info_Memo.Lines.Add('已连接……');
    end;procedure Tupdate_v.Update_NMHTTPConnectionFailed(Sender: TObject);
    begin
      Info_Memo.Lines.Add('连接失败!');
    end;procedure Tupdate_v.Update_NMHTTPDisconnect(Sender: TObject);
    begin
      Info_Memo.Lines.Add('断线中……');
    end;procedure Tupdate_v.Update_NMHTTPHostResolved(Sender: TComponent);
    begin
      Info_Memo.Lines.Add('正在解析地址……');
    end;procedure Tupdate_v.Update_NMHTTPStatus(Sender: TComponent; Status: String);
    begin
      Info_Memo.Lines.Add(Status);
      If Update_NMHTTP.ReplyNumber = 404 then
        Info_Memo.Lines.Add('更新文档没找到……');
    end;procedure Tupdate_v.Update_BtnClick(Sender: TObject);
    begin
      Info_Memo.Clear;
      Info_Memo.Lines.Add('系统消息:');
      Info_Memo.Lines.Add('开始查看更新信息……');
      Update_NMHTTP.InputFileMode := FALSE;
      Update_NMHTTP.OutputFileMode := FALSE;
      Update_NMHTTP.ReportLevel := Status_Basic;
      try
        Update_NMHTTP.Get(update_text);
        Info_Memo.Lines.Add('');
        Info_Memo.Lines.Add('开始分析……');
        if AnalyseUpdate(Update_NMHTTP.Body,Update_New) then
        begin
          NewVer_Label.Caption:=Format('%d.%d.%d.%d',[
            Update_New.Version.dwProductVersionMS shr 16,
            Update_New.Version.dwProductVersionMS and $FFFF,
            Update_New.Version.dwProductVersionLS shr 16,
            Update_New.Version.dwProductVersionLS and $FFFF
            ]);
          DownAddress1_Edit.Text:=Update_New.URL1;
          DownAddress2_Edit.Text:=Update_New.URL2;
          NewDate_Label.Caption:=DateToStr(Update_New.Date);
          UpdateInfo_Memo.Lines.Add(Update_New.Update_Info);
          Info_Memo.Lines.Add('已经分析成功!');
          DisplaySuggestion(True);
        end else
        begin
          Info_Memo.Lines.Add('不能找到更新信息!');
          Info_Memo.Lines.Add('分析失败!');
          DisplaySuggestion(False);
        end;
      except
        DisplaySuggestion(False);
        Application.MessageBox('程序更新失败!','提示',MB_OK);
      end;
    end;procedure Tupdate_v.FormCreate(Sender: TObject);
    begin
      Update_Ori.Name:='mossci';
      Update_Ori.Version:=GetBuildInfo(ParamStr(0));
      CurrentVerL_Label.Caption:=Format('%d.%d.%d.%d',[
        Update_Ori.Version.dwProductVersionMS shr 16,
        Update_Ori.Version.dwProductVersionMS and $FFFF,
        Update_Ori.Version.dwProductVersionLS shr 16,
        Update_Ori.Version.dwProductVersionLS and $FFFF
        ]);
    end;procedure Tupdate_v.DownAddress1_BtnClick(Sender: TObject);
    begin
      if DownAddress1_Edit.Text<>'' then
        ShellExecute(Handle,nil,PChar(DownAddress1_Edit.Text),nil,nil,SW_SHOWNORMAL);
    end;procedure Tupdate_v.DownAddress2_BtnClick(Sender: TObject);
    begin
      if DownAddress2_Edit.Text<>'' then
        ShellExecute(Handle,nil,PChar(DownAddress2_Edit.Text),nil,nil,SW_SHOWNORMAL);
    end;end.
      

  5.   

    update.txt 内容如下:;  用于检查版本更新的文件
    ;  update.txt File Format
    ;  First Application Update Information
    [mosstools]
      <ver>2.0.0.389</ver>
      <url1>http://delphibox.com/softm/3_update.zip</url1>
      <url2>http://delphibox.com/softm/3_update.zip</url2>
      <date>2002-4-26</date>
      <info>最新消息:天时地利!</info>
    [/mosstools];  Another Application Update Information
    [mossci]
      <ver>1.0.0.445</ver>
      <url1>http://202.115.144.37/~cornermoss/my/mossciv2.zip</url1>
      <url2>*********</url2>
      <date>2002-9-13</date>
      <info>最新消息:
    1. 填新词时加入了词格律;
    2. “我的作品”保存为文本文件时可选择是否保存格律;
    3. 对“我的作品”加入了平仄的校验。
    4. 显示当前光标所在的“行”和“列”
    </info>
    [/mossci][pedump]
      <ver>1.91.0.0</ver>
      <url>http://delphibox.com/softm/3_pedump.zip</url>
      <date>2002-2-25</date>
    [/pedump];  You can put more ...
    ;  Please upload this file to you website and change url just so so...
      

  6.   

    TNMHTTP在delphi 7.0中不存在,怎么版?
      

  7.   

    在网上放一个 ini文件,
    查找 ini 文件中的版本号是否比当前版本大,
    如果大的话,就关闭主程序,调用升级程序。我在ini文件是这样定义的。
    [new]
    version=20030701
    [20030701]
    mainfile=main.exe
    datafile=data.exe
    otherfile=other.exe我首先找ini文件,找到new,发现当前版本为20030701
    与现程序比较,发现比较新,
    然后再定位节点 20030701
    发现有3个文件需要更新,
    假定文件放在 www.sina.com.cn/upgrade/目录下
    然后用 idhttp.get('http://www.sina.com.cn/upgrade/main.exe')
    成功的话再
    idhttp.get('http://www.sina.com.cn/upgrade/data.exe')
    idhttp.get('http://www.sina.com.cn/upgrade/other.exe')不过这样由于服务器容易断线和其它socket错误。
    所以我做成一个zip文件,ini文件还是一样.
    但下载过程需要改一改
    首先用
    idhttp.get('http://www.sina.com.cn/upgrade/20030701.zip')20030701.zip里面的文件都写在 ini 文件的节点
    [20030701]
    mainfile=main.exe
    datafile=data.exe
    otherfile=other.exe然后在升级程序去把它解压,然后再覆盖文件,