C#
1、导入win32 api
[DllImport("wininet")]
public static extern int InternetGetConnectedState(ref int lpdwFlags, int dwReserved);
2、用法如下:
string InternetGetConnectedStateString()
{
string strState = "";
try
{
int nState = 0;
// check internet connection state
if(InternetGetConnectedState(ref nState, 0) == 0)
return "You are currently not connected to the internet";
if((nState & 1) == 1)
strState = "Modem connection";
else if((nState & 2) == 2)
strState = "LAN connection";
else if((nState & 4) == 4)
strState = "Proxy connection";
else if((nState & 8) == 8)
strState = "Modem is busy with a non-Internet connection";
else if((nState & 0x10) == 0x10)
strState = "Remote Access Server is installed";
else if((nState & 0x20) == 0x20)
return "Offline";
else if((nState & 0x40) == 0x40)
return "Internet connection is currently configured";
// get current machine IP
IPHostEntry he = Dns.Resolve(Dns.GetHostName());
strState += ", Machine IP: "+he.AddressList[0].ToString();
}
catch
{
}
return strState;
}
Keyword:网络连接情况InternetGetConnectedState C#