分享

【原创】delphi应用程序自动升级取真实文件名方法

 美好生活谷 2016-11-25

procedure Tflogin.Timer1Timer(Sender: TObject);
var
DownLoadFile:TFileStream;
myini: Tinifile;
myver, oldver, filename: string;
aURL: string;
IdHttp: TIdHttp;
begin
    Timer1.Enabled:=false;

    myini := TIniFile.Create(pchar(ExtractFilePath(Application.ExeName))+'user.ini');
    oldver := myini.ReadString('server','ver','100');
    myini.Free;

    aURL:='http://x.x.x.x/pub/downFile.do?fileid=2602';  //指向新的升级版本文件:up_2608.ini,其中2608为新的运行文件fileid
    IdHttp := TIdHttp.Create(self);
     // 处理重定向
    IdHttp.HandleRedirects := True;
    //IdHttp.Request.GetNamePath;
    IdHttp.Head(aURL);
    // 获取重定向后的URL 
    //aURL := IdHttp.URL.URI;

    //取真实文件名
    Filename:= IdHttp.Response.RawHeaders.Values['Content-Disposition'];
    Filename := copy(Filename,pos('=',Filename)+1,length(Filename));

    //获取Filename中的更新ID
    Filename:=copy(Filename,pos('_',Filename)+1,length(Filename));
    myver:=copy(Filename,1,pos('.',Filename)-1);
   


    if myver<>oldver then
      begin
       lbl_info.Caption := '发现新程序版本,更新...';
       //Button1.Enabled:=false;
      
       //检查jfglUpdate.exe存在
       filename:=pchar(ExtractFilePath(Application.ExeName))+'Update.exe';
       if not fileExists(filename)  then  //没有, 下载
          begin
            lbl_info.Caption:= '正在下载更新程序,请稍候...';
            DownLoadFile:=TFileStream.Create(pchar(ExtractFilePath(Application.ExeName))+'Update.exe',fmCreate);
            IdHTTP.Get('http://x.x.x.x/pub/downFile.do?fileid=2607',DownLoadFile);//2607指向Update.exe
            DownLoadFile.Free;
            Delay(2000);
          end;
      
       ShellExecute(0, nil, 'Update.exe', PChar(myver), nil, SW_SHOWNORMAL);
       application.Terminate;
      end;
end;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约