我正在学习 Inno Setup 来制作一个简单的安装程序。在安装过程中我需要从网站下载文件,因此检查是否有互联网连接很重要。在安装过程中如何检查或采取一些警报以连接互联网?
Thanks!
最好的检查是尝试实际下载该文件。
“互联网”几乎不是一个可以连接到的真实事物。因此,如果您连接到“互联网”,则很难进行测试。实际上,您不需要连接到“互联网”,您需要连接到您的服务器。所以测试一下。
See also
-
如何检查java中是否存在互联网连接? https://stackoverflow.com/q/1402005/850848
-
使用 .NET 检查 Internet 连接的最佳方法是什么? https://stackoverflow.com/q/2031824/850848
Inno Setup 中的等效实现如下:
function InitializeSetup(): Boolean;
var
WinHttpReq: Variant;
Connected: Boolean;
begin
Connected := False;
repeat
Log('Checking connection to the server');
try
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
{ Use your real server host name }
WinHttpReq.Open('GET', 'https://www.example.com/', False);
WinHttpReq.Send('');
Log('Connected to the server; status: ' + IntToStr(WinHttpReq.Status) + ' ' +
WinHttpReq.StatusText);
Connected := True;
except
Log('Error connecting to the server: ' + GetExceptionMessage);
if WizardSilent then
begin
Log('Connection to the server is not available, aborting silent installation');
Result := False;
Exit;
end
else
if MsgBox('Cannot reach server. Please check your Internet connection.',
mbError, MB_RETRYCANCEL) = IDRETRY then
begin
Log('Retrying');
end
else
begin
Log('Aborting');
Result := False;
Exit;
end;
end;
until Connected;
Result := True;
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)