[英]How to determine if network interface is connected to the Internet (i. e. the computer is online)
我需要一種以編程方式確定互聯網可用性的方法。 現在我用Ping來不斷ping一些網站。
但似乎Windows 7雖然以其他方式確定了互聯網可用性。 如果計算機處於聯機狀態,則系統托盤中的網絡接口圖標上會顯示地球圖標。
問題是:是否有任何標准的Win32方式來檢查在線狀態,贏取事件或其他什么,如果有,如何從C#中使用它?
雖然你的問題似乎是重復的,但我相信這樣的事情會奏效:
using System;
using System.Runtime;
using System.Runtime.InteropServices;
public class InternetCS
{
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
}
在這里:
除了調制解調器之外,“連接到互聯網”沒有任何實際意義。 測試任何資源是否可用的節拍方法是使用它。 無論如何,你必須應對失敗,不需要對所有內容進行兩次編碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.