WPF 检测计算机网络连接情况
private const int INTERNET_CONNECTION_MODEM = 1; private const int INTERNET_CONNECTION_LAN = 2; private const int INTERNET_CONNECTION_PROXY = 4; private const int INTERNET_CONNECTION_MODEM_BUSY = 8; [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
/// <summary> /// 检测用户计算机是否已连接网络 /// </summary> /// <returns></returns> public static bool IsOnLine() { try { var netstatus = string.Empty; var connection = 0; if (!InternetGetConnectedState(out connection, 0)) return false; if ((connection & INTERNET_CONNECTION_PROXY) != 0) netstatus += " 采用代理上网 \n"; if ((connection & INTERNET_CONNECTION_MODEM) != 0) netstatus += " 采用调治解调器上网 \n"; if ((connection & INTERNET_CONNECTION_LAN) != 0) netstatus += " 采用网卡上网 \n"; if ((connection & INTERNET_CONNECTION_MODEM_BUSY) != 0) netstatus += " MODEM被其他非INTERNET连接占用 \n"; Console.WriteLine(netstatus); return true; } catch (Exception) { return false; } }