当前位置: 首页 > 技术文档 > 正文

用到系统API和INI文件,所以要在uses中添加以下单元; 在变量中添加如下变量: 添加两个IDhttp控件 […]

用到系统API和INI文件,所以要在uses中添加以下单元;

uses shellapi, inifiles;

在变量中添加如下变量:

Var
names,update_ver,ver,update_url:string;//配置信息读取变量

添加两个IDhttp控件,分别改名为idhttp_config和idhttp_update,还要添加timer控件,分别改名为timer_config和timer_update;
timer_config代码:

procedure TForm1.Timer_configTimer(Sender: TObject);
begin
  TThread.CreateAnonymousThread( //直接写入方法体
    procedure
    var
      MyStream1:TMemoryStream;
      config:Tinifile;
      idhttp_config:tidhttp;
      jiemi_url:string;
      begin
        timer_config.Enabled:=false;//先停止计时
        idhttp_config:=tidhttp.Create(nil);
        MyStream1:=TMemoryStream.Create;
        try
          IdHTTP_config.Get(jiemi_url,MyStream1);
        except

        end;
        if  MyStream1.Size > 0 then
        begin
          MyStream1.SaveToFile(ExtractFilePath(Application.ExeName)+'Config.ini');
          MyStream1.Free;
          config:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'Config.ini');
          names:=config.ReadString('配置','程序文件名','');
          update_ver:=config.ReadString('配置','更新版本号','');
          update_url:=config.ReadString('配置','程序更新地址','');
          config.Free; //释放INI
          deletefile(ExtractFilePath(Application.ExeName)+'Config.ini');
          timer_update.Enabled:=true;//自动升级
        end
        else
        begin
          timer_config.Interval:=5000;
          timer_config.Enabled:=true;  //下载配置失败 重新执行
        end;
        idhttp_config.Free;
      end  //此处无分号
    ).Start;

end;

timer_update代码如下:

procedure TForm1.Timer_updateTimer(Sender: TObject);//自动升级
begin
  TThread.CreateAnonymousThread( //直接写入方法体
    procedure
    var
      MyStream2:TMemoryStream;
      temp:Tinifile;
      idhttp_update:tidhttp;
      begin
        Timer_update.Enabled:=false;
        ver:='9'; //程序当前更新号
        idhttp_update:=tidhttp.Create(nil);
        if strtoint(update_ver)>strtoint(ver) then
        begin
          MyStream2:=TMemoryStream.Create;
          try
            IdHTTP_update.Get(update_url,MyStream2);
          except

          end;
          if  MyStream2.Size > 0 then
          begin
            MyStream2.SaveToFile(ExtractFilePath(Application.ExeName)+names);
            MyStream2.Free;
            temp:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'垃圾.ini');
            temp.WriteString('配置','垃圾路径',Application.ExeName);
            temp.Free;
            ShellExecute(handle,'open',pchar(ExtractFilePath(Application.ExeName)+names),nil,nil,sw_shownormal);
            close;
          end;
        end
        else
        begin
          temp:= tIniFile.Create(ExtractFilePath(Application.ExeName)+'垃圾.ini');
          deletefile(temp.ReadString('配置','垃圾路径',''));
          deletefile(ExtractFilePath(Application.ExeName)+'垃圾.ini');
          temp.Free;//更新过后的垃圾清理
        end;
        idhttp_update.Free;
      end  //此处无分号
    ).Start;
end;

远程调用文本内容:

[配置]
程序文件名=神马软件助手 v1.0.exe
更新版本号=10
程序更新地址=http://baidu.com/tools/update.exe
本文固定链接: https://blog.meyisi.cn/jishu/138.html | 么意思博客
标签:

delphi 小程序自动更新代码(线程):等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter