从WPF / XBAP应用程序检测网络连接?

有没有办法确定XBAP(浏览器中托管的WPF)应用程序是否具有网络连接? 如果没有,用C#和.NET编写的传统Windows客户端如何确定它是否具有连接性?

基本上,用例是在通过WiFi连接到Intranet的移动笔记本电脑上运行的XBAP应用程序。 笔记本电脑将无法连接到Internet。 WiFi连接可能存在也可能不存在,这取决于用户当时的位置。

GateWayIPAddressInformation应该工作: http : //msdn.microsoft.com/en-us/library/system.net.networkinformation.gatewayipaddressinformation( loband) .aspx

如果您获得了网关IP地址,并且可以Ping( http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping ( loband ) .aspx ),那么您可能已经连接了。

在InitializeComponent()中执行此操作:

// Add EventHandler for NetworkAddressChanged event NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback); 

以下是该事件触发时调用的方法:

 internal void AddressChangedCallback(object sender, EventArgs e) { // Check for NetworkConnectivity _isInternetConnectionActive = new NetworkConnectivity().IsInternetConnected; } 

这是NetworkConnectivity类:

 public class NetworkConnectivity { private List _ipAddresses = new List(); public NetworkConnectivity() { _ipAddresses = new List(); } #region Public Properties public int CountIPAddresses { get { return this.IPAddresses.Count; } } public List IPAddresses { get { _ipAddresses.Clear(); // Get a listing of all network adapters NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; // If this adapter has at least 1 IPAddress if (addresses.Count > 0) { // Loop through all IP Addresses foreach (GatewayIPAddressInformation address in addresses) { _ipAddresses.Add(address.Address); } } } return _ipAddresses; } } public bool IsInternetConnected { get { if (this.CountIPAddresses == 0) { return false; } else { //IPAddress[] ips = ResolveDNSAddress("google.com"); //return PingIPAddressPool(ips); return PingIPAddress("72.14.204.104"); // Google IP } } } #endregion #region Public Methods public IPAddress[] ResolveDNSAddress(string UrlAddress) { IPHostEntry hostInfo = Dns.Resolve(UrlAddress); return hostInfo.AddressList; } public bool PingIPAddressPool(IPAddress[] ipAddresses) { foreach (IPAddress ip in ipAddresses) { if (PingIPAddress(ip.Address.ToString())) { return true; } } return false; } public bool PingIPAddress(string ip) { // Pinging IPAddress addr = IPAddress.Parse(ip); Ping pingSender = new Ping(); PingOptions options = new PingOptions(); // Use the default Ttl value which is 128, // but change the fragmentation behavior. options.DontFragment = true; // Create a buffer of 32 bytes of data to be transmitted. string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 15; // seconds to wait for response int attempts = 2; // ping attempts for (int i = 0; i < attempts; i++) { PingReply reply = pingSender.Send(addr, timeout, buffer, options); if (reply.Status == IPStatus.Success) { return true; } } return false; } #endregion }