Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a text file from the Internet resource?

I would like to read a text file containing a version number from the Internet resource. Then I need to use this version number within my script.

How to do this in InnoSetup ?

like image 940
Julien M Avatar asked Jan 14 '12 15:01

Julien M


1 Answers

There are many ways how to get a file from the Internet in InnoSetup. You can use an external library like for instance InnoTools Downloader, write your own library, or use one of the Windows COM objects. In the following example I've used the WinHttpRequest COM object for file receiving.

The DownloadFile function in this script returns True, when the WinHTTP functions doesn't raise any exception, False otherwise. The response content of the HTTP GET request to an URL, specified by the AURL parameter is then passed to a declared AResponse parameter. When the script fails the run on exception, AResponse parameter will contain the exception error message:

[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;
like image 112
TLama Avatar answered Oct 12 '22 11:10

TLama