本文整理自网络,侵删。
uses WinInet;
////网络是否连通function InternetConnected: Boolean;const // local system uses a modem to connect to the Internet. INTERNET_CONNECTION_MODEM = 1; // local system uses a local area network to connect to the Internet. INTERNET_CONNECTION_LAN = 2; // local system uses a proxy server to connect to the Internet. INTERNET_CONNECTION_PROXY = 4; // local system's modem is busy with a non-Internet connection. INTERNET_CONNECTION_MODEM_BUSY = 8;var dwConnectionTypes : DWORD;begin dwConnectionTypes := INTERNET_CONNECTION_MODEM+ INTERNET_CONNECTION_LAN + INTERNET_CONNECTION_PROXY; Result := InternetGetConnectedState(@dwConnectionTypes, 0);end;
{================================================================= 功 能: 检测机器是否登入网络 参 数: 无 返回值: 成功: True 失败: False=================================================================}Function NetCheckMacAttachNet: Boolean;begin Result := False; if GetSystemMetrics(SM_NETWORK) <> 0 then //所有连入网的 Result := True;end;
procedure SaveLog(const sMsg:string);var sList:TStringList; fn:string;begin sList := TStringList.Create; fn := ExtractFilePath(ParamStr(0))+'SysLog.txt'; try if FileExists(fn) then sList.LoadFromFile(fn); if sList.Count>1000 then sList.Clear; sList.Add(FormatDateTime('yyyy-mm-dd hh:nn:ss',Now)+' '+sMsg); sList.SaveToFile(fn); finally sList.Free; end;end;
调用:
try if NetCheckMacAttachNet and InternetConnected then // else begin // SaveLog('检测到网络连接中断!'); end; except on e:Exception do // SaveLog(e.Message); end;
相关阅读 >>
Delphi net.httpclient用最精简的代码获取网页数据
更多相关阅读请进入《Delphi》频道 >>