Some times you will need a mechanism to check whether changes to network occurring during running your application.
So as a solution for this you can add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented by theSystem.Net.NetworkInformation.NetworkChange class.

Souce Code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Add the handlers to the NetworkChange events.
            NetworkChange.NetworkAvailabilityChanged +=
            NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged +=
            NetworkAddressChanged;

Console.ReadLine();
        }

// Declare a method to handle NetworkAvailabilityChanged events.
        private static void NetworkAvailabilityChanged(
        object sender, NetworkAvailabilityEventArgs e)
        {
            // Report whether the network is now available or unavailable.
            if (e.IsAvailable)
            {
                Console.WriteLine("Network Available");
            }
            else
            {
                Console.WriteLine("Network Unavailable");
            }
        }
        // Declare a method to handle NetworkAdressChanged events.
        private static void NetworkAddressChanged(object sender, EventArgs e)
        {
            Console.WriteLine("Current IP Addresses:");
            // Iterate through the interfaces and display information.
            foreach (NetworkInterface ni in
            NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation addr
                in ni.GetIPProperties().UnicastAddresses)
                {
                    Console.WriteLine(" - {0} (lease expires {1})",
                    addr.Address, DateTime.Now +
                    new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
                }
            }
        }
    }
}


http://www.codeproject.com/Articles/206720/Simple-Network-Status-Monitor-Example

Detect Changes in Network Connectivity的更多相关文章

  1. ionic_ Network connectivity error occurred, are you offline?

    错误如下: HenHouse admin$ ionic cordova build ios --prod > ionic integrations enable cordova ✖ Downlo ...

  2. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  3. [转]Peer-to-Peer Communication Across Network Address Translators

    Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...

  4. Android开发训练之第五章——Building Apps with Connectivity & the Cloud

    Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...

  5. RabbitMQ Network Partitions

    Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you ...

  6. Managing Network Usage

    This lesson describes how to write applications that have fine-grained control over their usage of n ...

  7. Configure a bridged network interface for KVM using RHEL 5.4 or later?

    environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...

  8. Simple Network Management Protocol - SNMP Tutorial

    30.9 Simple Network Management Protocol Network management protocols specify communication between t ...

  9. How Network Load Balancing Technology Works--reference

    http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...

随机推荐

  1. Eclipse连接sqlserver体验过程

    以前装的sqlserver 2008试用期到了,就按照网上的步骤,彻底删除了sqlserver,然后又重新装了下,再用eclipse连接的时候,发现提示TCP/IP被禁用,然后找到了sqlserver ...

  2. javascript event visualize

    很多时候拿到一个spa,特别是基于jquery的比较复杂的spa时,如果你好奇他是如何工作的,往往没有头绪. 由于spa基本上都是基于事件触发的,一个可行的办法是通过查看事件处理代码能够对spa有一个 ...

  3. [SQL SERVER] 映射网络驱动器,让SQL服务器可见

    在服务器上运行: EXEC sp_configure 'show advanced options', 1; GO RECONFIGURE; GO EXEC sp_configure 'xp_cmds ...

  4. Python学习---IO的异步[asyncio +aiohttp模块]

    aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...

  5. 最大公约数(GCD)与最小公倍数(LCM)的计算

    给出两个数a.b,求最大公约数(GCD)与最小公倍数(LCM) 一.最大公约数(GCD)    最大公约数的递归:  * 1.若a可以整除b,则最大公约数是b  * 2.如果1不成立,最大公约数便是b ...

  6. print in或者not in, 判断在不在里面

    print("不疼" in "麻花疼")        # 结果False print("不疼"in "真不疼") # ...

  7. Keepalived 实现双机热备

    原理 首先有一个虚拟ip暴露给客户端,虚拟ip对应的mac地址为一台真实服务器, 即用户向虚拟ip发送一个请求,该请求会被分发到真实服务器上. 现在有2台真实服务器,一台master,一台backup ...

  8. 【连接】Spring事物的传播行为

    http://blog.csdn.net/it_wangxiangpan/article/details/24180085 PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务, ...

  9. ceph crush算法和crushmap浅析

    1 什么是crushmap crushmap就相当于是ceph集群的一张数据分布地图,crush算法通过该地图可以知道数据应该如何分布:找到数据存放位置从而直接与对应的osd进行数据访问和写入:故障域 ...

  10. Matlab面向对象编程基础

    DeepLab是一款基于Matlab面向对象编程的深度学习工具箱,所以了解Matlab面向对象编程的特点是必要的.笔者在做Matlab面向对象编程的时候发现无论是互联网上还是书店里卖的各式Matlab ...