如何从Internet资源中读取文本文件?

Jul*_*n M 4 inno-setup download

我想从Internet资源中读取包含版本号的文本文件.然后我需要在我的脚本中使用此版本号.

如何在InnoSetup中做到这一点?

TLa*_*ama 10

如何从InnoSetup中的Internet获取文件有很多方法.您可以使用外部库,例如InnoTools Downloader编写自己的库,或使用其中一个Windows COM对象.在下面的示例中,我使用WinHttpRequestCOM对象进行文件接收.

DownloadFile当WinHTTP函数没有引发任何异常时,此脚本中的函数返回True,否则返回False.然后,由AURL参数指定的HTTP GET请求对URL的响应内容将传递给声明的AResponse参数.当脚本在异常时运行失败时,AResponse参数将包含异常错误消息:

[Code]
function DownloadFile(const AURL: string; var AResponse: string): Boolean;
var
  WinHttpRequest: Variant;
begin
  Result := True;
  try
    WinHttpRequest := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    WinHttpRequest.Open('GET', AURL, False);
    WinHttpRequest.Send;
    AResponse := WinHttpRequest.ResponseText;
  except
    Result := False;
    AResponse := GetExceptionMessage;
  end;
end;

procedure InitializeWizard;
var
  S: string;
begin
  if DownloadFile('http://www.example.com/versioninfo.txt', S) then
    MsgBox(S, mbInformation, MB_OK)
  else
    MsgBox(S, mbError, MB_OK)
end;
Run Code Online (Sandbox Code Playgroud)